Whamcloud - gitweb
fixes:
[fs/lustre-release.git] / lustre / mds / mds_capa.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2004, 2005 Cluster File Systems, Inc.
5  *
6  * Author: Lai Siyao <lsy@clusterfs.com>
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_MDS
25
26 #include <linux/fs.h>
27 #include <linux/version.h>
28 #include <asm/uaccess.h>
29 #include <linux/file.h>
30 #include <linux/kmod.h>
31 #include <linux/random.h>
32
33 #include <linux/obd.h>
34 #include <linux/lustre_mds.h>
35 #include <linux/lustre_fsfilt.h>
36 #include <linux/lustre_sec.h>
37
38 #include "mds_internal.h"
39
40 static struct ptlrpc_thread mds_eck_thread;
41
42 static struct thread_ctl {
43         struct completion ctl_starting;
44         struct completion ctl_finishing;
45 } mds_eck_ctl;
46
47 static LIST_HEAD(mds_capa_key_list);
48 static spinlock_t mds_capa_lock; /* protect capa and capa key */
49 struct timer_list mds_eck_timer;
50
51 #define CAPA_KEY_JIFFIES(key) \
52         expiry_to_jiffies(le64_to_cpu((key)->k_key->lk_expiry))
53
54 #define CUR_MDS_CAPA_KEY(mds) (mds)->mds_capa_keys[(mds)->mds_capa_key_idx]
55 #define CUR_CAPA_KEY(mds) CUR_MDS_CAPA_KEY(mds).k_key
56 #define CUR_CAPA_KEY_ID(mds) CUR_MDS_CAPA_KEY(mds).k_key->lk_keyid
57 #define CUR_CAPA_KEY_LIST(mds) CUR_MDS_CAPA_KEY(mds).k_list
58 #define CUR_CAPA_KEY_EXPIRY(mds) le64_to_cpu(CUR_CAPA_KEY(mds)->lk_expiry)
59 #define CUR_CAPA_KEY_JIFFIES(mds) CAPA_KEY_JIFFIES(&CUR_MDS_CAPA_KEY(mds))
60
61 static int mds_write_capa_key(struct obd_device *obd, int force_sync)
62 {
63         struct mds_obd *mds = &obd->u.mds;
64         struct mds_capa_key *keys = mds->mds_capa_keys;
65         struct file *filp = mds->mds_capa_keys_filp;
66         struct lvfs_run_ctxt saved;
67         loff_t off = 0;
68         int i, rc = 0;
69         ENTRY;
70
71         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
72         for (i = 0; i < 2 && keys[i].k_key; i++) {
73                 rc = fsfilt_write_record(obd, filp, keys[i].k_key,
74                                          sizeof(*keys[i].k_key),
75                                          &off, force_sync);
76                 if (rc) {
77                         CERROR("error writing MDS capa key: rc = %d\n", rc);
78                         break;
79                 }
80         }
81         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
82
83         RETURN(rc);
84 }
85
86 static inline int
87 mds_capa_key_cmp(struct mds_obd *mds)
88 {
89         return le32_to_cpu(mds->mds_capa_keys[0].k_key->lk_keyid) -
90                le32_to_cpu(mds->mds_capa_keys[1].k_key->lk_keyid);
91 }
92
93 static void
94 do_update_capa_key(struct mds_obd *mds, struct lustre_capa_key *key)
95 {
96         __u32 keyid = 1;
97         __u64 expiry_rounded;
98
99         if (CUR_CAPA_KEY(mds))
100                 keyid = le32_to_cpu(CUR_CAPA_KEY_ID(mds)) + 1;
101         spin_lock(&mds_capa_lock);
102         expiry_rounded = round_expiry(mds->mds_capa_key_timeout);
103         spin_unlock(&mds_capa_lock);
104
105         key->lk_mdsid = cpu_to_le32(mds->mds_num);
106         key->lk_keyid = cpu_to_le32(keyid);
107         key->lk_expiry = cpu_to_le64(expiry_rounded);
108         get_random_bytes(key->lk_key, sizeof(key->lk_key));
109 }
110
111 static void list_add_capa_key(struct mds_capa_key *key, struct list_head *head)
112 {
113         struct mds_capa_key *tmp;
114
115         list_for_each_entry_reverse(tmp, head, k_list) {
116                 if (le64_to_cpu(key->k_key->lk_expiry) <
117                     le64_to_cpu(tmp->k_key->lk_expiry)) {
118                         /* put key before tmp */
119                         list_add_tail(&key->k_list, &tmp->k_list);
120                         return;
121                 }
122         }
123
124         list_add_tail(&key->k_list, head);
125 }
126
127 int mds_read_capa_key(struct obd_device *obd, struct file *file)
128 {
129         loff_t off = 0;
130         struct mds_obd *mds = &obd->u.mds;
131         struct lustre_capa_key *key;
132         unsigned long capa_keys_size = file->f_dentry->d_inode->i_size;
133         unsigned long expiry;
134         int i = 0, rc = 0;
135         ENTRY;
136
137         if (capa_keys_size == 0) {
138                 CWARN("%s: initializing new %s\n", obd->obd_name,
139                       file->f_dentry->d_name.name);
140                  
141                 OBD_ALLOC(key, sizeof(*key));
142                 if (!key)
143                         RETURN(-ENOMEM);
144
145                 do_update_capa_key(mds, key);
146
147                 mds->mds_capa_keys[0].k_key = key;
148                 mds->mds_capa_keys[0].k_obd = obd;
149                 INIT_LIST_HEAD(&mds->mds_capa_keys[0].k_list);
150                 mds->mds_capa_key_idx = 0;
151
152                 rc = mds_write_capa_key(obd, 1);
153                 if (rc)
154                         GOTO(out, rc);
155         } else {
156                 LASSERT(capa_keys_size == sizeof(*key) ||
157                         capa_keys_size == 2 * sizeof(*key));
158
159                 while (capa_keys_size > i * sizeof(*key)) {
160                         OBD_ALLOC(key, sizeof(*key));
161                         if (!key)
162                                 RETURN(-ENOMEM);
163
164                         rc = fsfilt_read_record(obd, file, key, sizeof(*key),
165                                                 &off);
166                         if (rc) {
167                                 CERROR("error reading MDS %s capa key: %d\n",
168                                        file->f_dentry->d_name.name, rc);
169                                 OBD_FREE(key, sizeof(*key));
170                                 GOTO(out, rc);
171                         }
172
173                         mds->mds_capa_keys[i].k_key = key;
174                         mds->mds_capa_keys[i].k_obd = obd;
175                         INIT_LIST_HEAD(&mds->mds_capa_keys[i].k_list);
176                         i++;
177                 }
178
179                 mds->mds_capa_key_idx = 0;
180                 if (mds->mds_capa_keys[1].k_key && mds_capa_key_cmp(mds) < 0)
181                         mds->mds_capa_key_idx = 1;
182         }
183
184         expiry = CUR_CAPA_KEY_JIFFIES(mds);
185         spin_lock(&mds_capa_lock);
186         if (time_before(expiry, mds_eck_timer.expires) ||
187             !timer_pending(&mds_eck_timer))
188                 mod_timer(&mds_eck_timer, expiry);
189         list_add_capa_key(&CUR_MDS_CAPA_KEY(mds), &mds_capa_key_list);
190         spin_unlock(&mds_capa_lock);
191 out:
192         RETURN(rc);
193 }
194
195 void mds_capa_keys_cleanup(struct obd_device *obd)
196 {
197         struct mds_obd *mds = &obd->u.mds;
198         int i;
199
200         del_timer(&mds_eck_timer);
201         spin_lock(&mds_capa_lock);
202         if (CUR_CAPA_KEY(mds))
203                 list_del_init(&CUR_CAPA_KEY_LIST(mds));
204         spin_unlock(&mds_capa_lock);
205
206         for (i = 0; i < 2; i++)
207                 if (mds->mds_capa_keys[i].k_key)
208                         OBD_FREE(mds->mds_capa_keys[i].k_key,
209                                  sizeof(struct lustre_capa_key));
210 }
211
212 static int mds_set_capa_key(struct obd_device *obd, struct lustre_capa_key *key)
213 {
214         struct mds_obd *mds = &obd->u.mds;
215         int rc;
216         ENTRY;
217
218         rc = obd_set_info(mds->mds_dt_exp, strlen("capa_key"), "capa_key",
219                           sizeof(*key), key);
220         RETURN(rc);
221 }
222
223 static int
224 mds_update_capa_key(struct obd_device *obd, struct mds_capa_key *mkey,
225                     int force_sync)
226 {
227         struct mds_obd *mds = &obd->u.mds;
228         int to_update = !mds->mds_capa_key_idx;
229         struct lustre_capa_key *key = mds->mds_capa_keys[to_update].k_key;
230         __u32 keyid;
231         unsigned long expiry;
232         int rc, rc2;
233         ENTRY;
234
235         LASSERT(mkey != &mds->mds_capa_keys[to_update]);
236
237         if (key == NULL) {
238                 /* first update */
239                 OBD_ALLOC(key, sizeof(*key));
240                 if (!key)
241                         RETURN(-ENOMEM);
242                 mds->mds_capa_keys[to_update].k_key = key;
243                 mds->mds_capa_keys[to_update].k_obd = obd;
244         }
245
246         do_update_capa_key(mds, key);
247
248         keyid = le32_to_cpu(key->lk_keyid);
249
250         rc = mds_set_capa_key(obd, key);
251         if (rc)
252                 /* XXX: anyway, it will be replayed */
253                 CERROR("error set capa key(id %u), err = %d\n", keyid, rc);
254
255         rc2 = mds_write_capa_key(obd, 1);
256         if (rc2)
257                 GOTO(out, rc2);
258         
259         CDEBUG(D_INFO, "wrote capa keyid %u\n", keyid);
260
261         spin_lock(&mds_capa_lock);
262         list_del_init(&CUR_CAPA_KEY_LIST(mds));
263         mds->mds_capa_key_idx = to_update;
264         expiry = CUR_CAPA_KEY_JIFFIES(mds);
265         list_add_capa_key(&CUR_MDS_CAPA_KEY(mds), &mds_capa_key_list);
266
267         if (time_before(expiry, mds_eck_timer.expires) ||
268             !timer_pending(&mds_eck_timer))
269                 mod_timer(&mds_eck_timer, expiry);
270         spin_unlock(&mds_capa_lock);
271
272         DEBUG_MDS_CAPA_KEY(D_INFO, &CUR_MDS_CAPA_KEY(mds),
273                            "mds_update_capa_key");
274 out:
275         RETURN(rc2);
276 }
277
278 static inline int have_expired_capa_key(void)
279 {
280         struct mds_capa_key *key;
281         int expired = 0;
282         ENTRY;
283
284         spin_lock(&mds_capa_lock);
285         if (!list_empty(&mds_capa_key_list)) {
286                 key = list_entry(mds_capa_key_list.next, struct mds_capa_key,
287                                  k_list);
288                 /* expiry is in sec, so in case it misses, the result will
289                  * minus HZ and then compare with jiffies. */
290                 expired = time_before(CAPA_KEY_JIFFIES(key) - HZ, jiffies);
291         }
292         spin_unlock(&mds_capa_lock);
293
294         RETURN(expired);
295 }
296
297 static int inline mds_capa_key_check_stop(void)
298 {
299         return (mds_eck_thread.t_flags & SVC_STOPPING) ? 1: 0;
300 }
301
302 static int mds_capa_key_thread_main(void *arg)
303 {
304         struct thread_ctl *ctl = arg;
305         unsigned long flags;
306         int rc;
307         ENTRY;
308
309         lock_kernel();
310         ptlrpc_daemonize();
311
312         SIGNAL_MASK_LOCK(current, flags);
313         sigfillset(&current->blocked);
314         RECALC_SIGPENDING;
315         SIGNAL_MASK_UNLOCK(current, flags);
316         THREAD_NAME(current->comm, sizeof(current->comm), "mds_ck");
317         unlock_kernel();
318
319         /*
320          * letting starting function know, that we are ready and control may be
321          * returned.
322          */
323         mds_eck_thread.t_flags = SVC_RUNNING;
324         complete(&ctl->ctl_starting);
325
326         while (!mds_capa_key_check_stop()) {
327                 struct l_wait_info lwi = { 0 };
328                 struct mds_capa_key *key, *tmp, *next = NULL;
329
330                 l_wait_event(mds_eck_thread.t_ctl_waitq,
331                              (have_expired_capa_key() ||
332                               mds_capa_key_check_stop()),
333                              &lwi);
334
335                 spin_lock(&mds_capa_lock);
336                 list_for_each_entry_safe(key, tmp, &mds_capa_key_list, k_list) {
337                         if (time_after(CAPA_KEY_JIFFIES(key), jiffies)) {
338                                 next = key;
339                                 break;
340                         }
341
342                         spin_unlock(&mds_capa_lock);
343
344                         CDEBUG(D_INFO, "mds capa key expired: "
345                                "mds #%u, key #%u\n",
346                                le32_to_cpu(key->k_key->lk_mdsid),
347                                le32_to_cpu(key->k_key->lk_keyid));
348
349                         rc = mds_update_capa_key(key->k_obd, key, 1);
350                         spin_lock(&mds_capa_lock);
351                 }
352
353                 if (next)
354                         mod_timer(&mds_eck_timer, CAPA_KEY_JIFFIES(next));
355                 spin_unlock(&mds_capa_lock);
356         }
357
358         mds_eck_thread.t_flags = SVC_STOPPED;
359
360         /* this is SMP-safe way to finish thread. */
361         complete_and_exit(&ctl->ctl_finishing, 0);
362         EXIT;
363 }
364
365 void mds_capa_key_timer_callback(unsigned long unused)
366 {
367         ENTRY;
368         wake_up(&mds_eck_thread.t_ctl_waitq);
369         EXIT;
370 }
371
372 int mds_capa_key_start_thread(void)
373 {
374         int rc;
375         ENTRY;
376
377         LASSERT(mds_eck_thread.t_flags == 0);
378         init_completion(&mds_eck_ctl.ctl_starting);
379         init_completion(&mds_eck_ctl.ctl_finishing);
380         init_waitqueue_head(&mds_eck_thread.t_ctl_waitq);
381         spin_lock_init(&mds_capa_lock);
382
383         rc = kernel_thread(mds_capa_key_thread_main, &mds_eck_ctl,
384                            (CLONE_VM | CLONE_FILES));
385         if (rc < 0) {
386                 CERROR("cannot start capa key thread, "
387                        "err = %d\n", rc);
388                 RETURN(rc);
389         }
390
391         wait_for_completion(&mds_eck_ctl.ctl_starting);
392         LASSERT(mds_eck_thread.t_flags == SVC_RUNNING);
393         RETURN(0);
394 }
395
396 void mds_capa_key_stop_thread(void)
397 {
398         ENTRY;
399         mds_eck_thread.t_flags = SVC_STOPPING;
400         wake_up(&mds_eck_thread.t_ctl_waitq);
401         wait_for_completion(&mds_eck_ctl.ctl_finishing);
402         LASSERT(mds_eck_thread.t_flags == SVC_STOPPED);
403         mds_eck_thread.t_flags = 0;
404         EXIT;
405 }
406
407 void mds_update_capa_stat(struct obd_device *obd, int stat)
408 {
409         struct mds_obd *mds = &obd->u.mds;
410
411         spin_lock(&mds_capa_lock);
412         mds->mds_capa_stat = stat;
413         spin_unlock(&mds_capa_lock);
414 }
415
416 void mds_update_capa_timeout(struct obd_device *obd, unsigned long timeout)
417 {
418         struct mds_obd *mds = &obd->u.mds;
419
420         spin_lock(&mds_capa_lock);
421         mds->mds_capa_timeout = timeout;
422         /* XXX: update all capabilities in cache if their expiry too long */
423         spin_unlock(&mds_capa_lock);
424 }
425
426 int mds_update_capa_key_timeout(struct obd_device *obd, unsigned long timeout)
427 {
428         struct mds_obd *mds = &obd->u.mds;
429         struct timeval tv;
430         int rc;
431         ENTRY;
432
433         do_gettimeofday(&tv);
434
435         spin_lock(&mds_capa_lock);
436         mds->mds_capa_key_timeout = timeout;
437         if (CUR_CAPA_KEY_EXPIRY(mds) < tv.tv_sec + timeout) {
438                 spin_unlock(&mds_capa_lock);
439                 RETURN(0);
440         }
441         spin_unlock(&mds_capa_lock);
442
443         rc = mds_update_capa_key(obd, &CUR_MDS_CAPA_KEY(mds), 1);
444
445         RETURN(rc);
446 }
447
448 static void mds_capa_reverse_map(struct mds_export_data *med,
449                                  struct lustre_capa *capa)
450 {
451         uid_t uid;
452
453         if (!med->med_remote)
454                 return;
455
456         ENTRY;
457         uid = mds_idmap_lookup_uid(med->med_idmap, 1, capa->lc_uid);
458         if (uid == MDS_IDMAP_NOTFOUND)
459                 uid = med->med_nllu;
460         capa->lc_ruid = uid;
461         capa->lc_flags |= CAPA_FL_REMUID;
462         EXIT;
463 }
464
465
466 int mds_pack_capa(struct obd_device *obd, struct mds_export_data *med,
467                   struct mds_body *req_body, struct lustre_capa *req_capa,
468                   struct ptlrpc_request *req, int *offset, struct mds_body *body)
469 {
470         struct mds_obd *mds = &obd->u.mds;
471         struct lustre_capa *capa;
472         struct lustre_msg *repmsg = req->rq_repmsg;
473         struct obd_capa *ocapa;
474         __u8 key[CAPA_KEY_LEN];  /* key */
475         int stat, expired, rc = 0;
476         ENTRY;
477
478         spin_lock(&mds_capa_lock);
479         stat = mds->mds_capa_stat;
480         spin_unlock(&mds_capa_lock);
481
482         if (stat == 0) {
483                 (*offset)++;
484                 RETURN(0); /* capability is disabled */
485         }
486
487         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_PACK_CAPA))
488                 RETURN(-EINVAL);
489
490         if (req_body) {
491                 /* capa renewal, check capa op against open mode */
492                 struct mds_file_data *mfd;
493                 int mode;
494
495                 mfd = mds_handle2mfd(&req_body->handle);
496                 if (mfd == NULL) {
497                         CERROR("no handle for capa renewal ino "LPD64
498                                ": cookie "LPX64"\n",
499                                req_capa->lc_ino, req_body->handle.cookie);
500                         RETURN(-ESTALE);
501                 }
502
503                 mode = accmode(mfd->mfd_mode);
504                 if (!(req_capa->lc_op & mode)) {
505                         CERROR("invalid capa to renew ino "LPD64
506                                ": op %d mismatch with mode %d\n",
507                                req_capa->lc_ino, req_capa->lc_op,
508                                mfd->mfd_mode);
509                         RETURN(-EACCES);
510                 }
511         }
512
513         LASSERT(repmsg->buflens[*offset] == sizeof(*capa));
514         capa = lustre_msg_buf(repmsg, (*offset)++, sizeof(*capa));
515         LASSERT(capa != NULL);
516
517         ocapa = capa_get(req_capa->lc_uid, req_capa->lc_op, req_capa->lc_mdsid,
518                          req_capa->lc_ino, MDS_CAPA, NULL, NULL, NULL);
519         if (ocapa) {
520                 expired = capa_is_to_expire(ocapa);
521                 if (!expired) {
522                         capa_dup(capa, ocapa);
523                         capa_put(ocapa);
524                         GOTO(out, rc);
525                 }
526                 capa_put(ocapa);
527         }
528
529         memcpy(capa, req_capa, sizeof(*capa));
530         mds_capa_reverse_map(med, capa);
531
532         spin_lock(&mds_capa_lock);
533         capa->lc_keyid = le32_to_cpu(CUR_CAPA_KEY_ID(mds));
534         capa->lc_expiry = round_expiry(mds->mds_capa_timeout);
535         if (mds->mds_capa_timeout < CAPA_EXPIRY)
536                 capa->lc_flags |= CAPA_FL_NOROUND;
537         memcpy(key, CUR_CAPA_KEY(mds)->lk_key, sizeof(key));
538         spin_unlock(&mds_capa_lock);
539
540         capa_hmac(mds->mds_capa_hmac, key, capa);
541
542         ocapa = capa_renew(capa, MDS_CAPA);
543         if (!ocapa)
544                 rc = -ENOMEM;
545 out:
546         if (rc == 0)
547                 body->valid |= OBD_MD_CAPA;
548         RETURN(rc);
549 }