Whamcloud - gitweb
4bebb59fb47fd39428c94a476721cb5aa0a19f45
[fs/lustre-release.git] / lustre / llite / llite_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_LLITE
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
32 #include <linux/lustre_lite.h>
33 #include "llite_internal.h"
34
35 static struct ptlrpc_thread ll_capa_thread;
36 static struct list_head *ll_capa_list = &capa_list[CLIENT_CAPA];
37 static struct thread_ctl {
38         struct completion ctl_starting;
39         struct completion ctl_finishing;
40 } ll_capa_ctl;
41
42 static inline int have_expired_capa(void)
43 {
44         struct obd_capa *ocapa;
45         int expired = 0;
46         ENTRY;
47
48         spin_lock(&capa_lock);
49         if (!list_empty(ll_capa_list)) {
50                 ocapa = list_entry(ll_capa_list->next, struct obd_capa, c_list);
51
52                 expired = __capa_is_to_expire(ocapa);
53         }
54         spin_unlock(&capa_lock);
55
56         RETURN(expired);
57 }
58
59 static int inline ll_capa_check_stop(void)
60 {
61         return (ll_capa_thread.t_flags & SVC_STOPPING) ? 1: 0;
62 }
63
64 static int ll_renew_capa(struct obd_capa *ocapa)
65 {
66         struct ptlrpc_request *req = NULL;
67         /* no need to lock, no one else will touch it */
68         struct inode *inode = ocapa->c_inode;
69         struct obd_export *md_exp = ll_i2mdexp(inode);
70         struct ll_inode_info *lli = ll_i2info(inode);
71         __u64 valid = 0;
72         int rc;
73         ENTRY;
74
75         valid |= OBD_MD_CAPA;
76
77         rc = md_getattr(md_exp, &lli->lli_id, valid, NULL, NULL, 0,
78                         0, ocapa, &req);
79         if (rc < 0)
80                 CDEBUG(D_INFO, "md_getattr failed: rc = %d\n", rc);
81         RETURN(rc);
82 }
83
84 static int ll_capa_thread_main(void *arg)
85 {
86         struct thread_ctl *ctl = arg;
87         unsigned long flags;
88         ENTRY;
89
90         {
91                 char name[sizeof(current->comm)];
92                 snprintf(name, sizeof(name) - 1, "ll_capa");
93                 kportal_daemonize(name);
94         }
95
96         SIGNAL_MASK_LOCK(current, flags);
97         sigfillset(&current->blocked);
98         RECALC_SIGPENDING;
99         SIGNAL_MASK_UNLOCK(current, flags);
100
101         /*
102          * letting starting function know, that we are ready and control may be
103          * returned.
104          */
105         ll_capa_thread.t_flags = SVC_RUNNING;
106         complete(&ctl->ctl_starting);
107
108         while (1) {
109                 struct l_wait_info lwi = { 0 };
110                 struct obd_capa *ocapa, *next = NULL;
111                 int sleep = CAPA_PRE_EXPIRY;
112
113                 l_wait_event(ll_capa_thread.t_ctl_waitq,
114                              (have_expired_capa() || ll_capa_check_stop()),
115                              &lwi);
116
117                 spin_lock(&capa_lock);
118                 list_for_each_entry(ocapa, ll_capa_list, c_list) {
119                         if (__capa_is_to_expire(ocapa)) {
120                                 /* get capa in case it's deleted */
121                                 __capa_get(ocapa);
122
123                                 spin_unlock(&capa_lock);
124                                 ll_renew_capa(ocapa);
125                                 capa_put(ocapa, CLIENT_CAPA);
126                                 spin_lock(&capa_lock);
127                         } else {
128                                 next = ocapa;
129                                 break;
130                         }
131                 }
132                 if (next) {
133                         mod_timer(&ll_capa_timer,
134                                   expiry_to_jiffies(next->c_capa.lc_expiry));
135                         if (next->c_capa.lc_flags & CAPA_FL_NOROUND)
136                                 sleep = CAPA_PRE_EXPIRY_NOROUND;
137                 }
138                 spin_unlock(&capa_lock);
139
140                 if (ll_capa_check_stop())
141                         break;
142
143                 /* wait ll_renew_capa finish */
144                 set_current_state(TASK_INTERRUPTIBLE);
145                 schedule_timeout(sleep * HZ);
146         }
147
148         ll_capa_thread.t_flags = SVC_STOPPED;
149
150         /* this is SMP-safe way to finish thread. */
151         complete_and_exit(&ctl->ctl_finishing, 0);
152         EXIT;
153 }
154
155 /* just wake up, others are handled by ll_capa_thread */
156 void ll_capa_timer_callback(unsigned long unused)
157 {
158         ENTRY;
159         wake_up(&ll_capa_thread.t_ctl_waitq);
160         EXIT;
161 }
162
163 int ll_capa_start_thread(void)
164 {
165         int rc;
166         ENTRY;
167
168         LASSERT(ll_capa_thread.t_flags == 0);
169         init_completion(&ll_capa_ctl.ctl_starting);
170         init_completion(&ll_capa_ctl.ctl_finishing);
171         init_waitqueue_head(&ll_capa_thread.t_ctl_waitq);
172
173         rc = kernel_thread(ll_capa_thread_main, &ll_capa_ctl,
174                            (CLONE_VM | CLONE_FILES));
175         if (rc < 0) {
176                 CERROR("cannot start expired capa thread, "
177                        "err = %d\n", rc);
178                 RETURN(rc);
179         }
180         wait_for_completion(&ll_capa_ctl.ctl_starting);
181         LASSERT(ll_capa_thread.t_flags == SVC_RUNNING);
182         RETURN(0);
183 }
184
185 void ll_capa_stop_thread(void)
186 {
187         ENTRY;
188
189         ll_capa_thread.t_flags = SVC_STOPPING;
190         wake_up(&ll_capa_thread.t_ctl_waitq);
191         wait_for_completion(&ll_capa_ctl.ctl_finishing);
192         LASSERT(ll_capa_thread.t_flags == SVC_STOPPED);
193         ll_capa_thread.t_flags = 0;
194
195         EXIT;
196 }
197
198 int ll_set_och_capa(struct inode *inode, struct lookup_intent *it,
199                     struct obd_client_handle *och)
200 {
201         struct ptlrpc_request *req = LUSTRE_IT(it)->it_data;
202         struct ll_inode_info *lli = ll_i2info(inode);
203         struct mds_body *body;
204         struct lustre_capa *capa;
205         __u64 mdsid = lli->lli_id.li_fid.lf_group;
206         unsigned long ino = lli->lli_id.li_stc.u.e3s.l3s_ino;
207         int capa_op = (it->it_flags & MAY_WRITE) ? MAY_WRITE : MAY_READ;
208         unsigned long expiry;
209         int rc = 0;
210         ENTRY;
211
212         body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
213         LASSERT(body != NULL);          /* reply already checked out */
214         LASSERT_REPSWABBED(req, 1);     /* and swabbed down */
215
216         capa = lustre_msg_buf(req->rq_repmsg, 7, sizeof (*capa));
217         LASSERT(capa != NULL);          /* reply already checked out */
218         LASSERT_REPSWABBED(req, 7);     /* and swabbed down */
219
220         och->och_capa = capa_get(current->uid, capa_op, mdsid, ino,
221                                  CLIENT_CAPA, capa, inode, &body->handle);
222         if (!och->och_capa)
223                 rc = -ENOMEM;
224
225         expiry = expiry_to_jiffies(capa->lc_expiry - capa_pre_expiry(capa));
226         if (time_before(expiry, ll_capa_timer.expires) ||
227             !timer_pending(&ll_capa_timer))
228                 mod_timer(&ll_capa_timer, expiry);
229
230         RETURN(rc);
231 }