Whamcloud - gitweb
- added more comments in GNS stuff about possible odd conditions
[fs/lustre-release.git] / lustre / llite / llite_gns.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: Phil Schwan <phil@clusterfs.com>
7  * Author: Oleg Drokin <green@clusterfs.com>
8  * Author: Yury Umanets <yury@clusterfs.com>
9  * Review: Nikita Danilov <nikita@clusterfs.com>
10  *
11  *   This file is part of Lustre, http://www.lustre.org.
12  *
13  *   Lustre is free software; you can redistribute it and/or
14  *   modify it under the terms of version 2 of the GNU General Public
15  *   License as published by the Free Software Foundation.
16  *
17  *   Lustre is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU General Public License for more details.
21  *
22  *   You should have received a copy of the GNU General Public License
23  *   along with Lustre; if not, write to the Free Software
24  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  */
26
27 #define DEBUG_SUBSYSTEM S_LLITE
28
29 #include <linux/fs.h>
30 #include <linux/version.h>
31 #include <asm/uaccess.h>
32 #include <linux/file.h>
33 #include <linux/kmod.h>
34
35 #include <linux/lustre_lite.h>
36 #include "llite_internal.h"
37
38 static struct list_head gns_sbi_list = LIST_HEAD_INIT(gns_sbi_list);
39 static spinlock_t gns_lock = SPIN_LOCK_UNLOCKED;
40 static struct ptlrpc_thread gns_thread;
41 static struct ll_gns_ctl gns_ctl;
42
43 /*
44  * waits until passed dentry gets mountpoint or timeout and attempts are
45  * exhausted. Returns 1 if dentry became mountpoint and 0 otherwise.
46  */
47 static int
48 ll_gns_wait_for_mount(struct dentry *dentry,
49                       int timeout, int tries)
50 {
51         struct l_wait_info lwi;
52         struct ll_sb_info *sbi;
53         ENTRY;
54
55         LASSERT(dentry != NULL);
56         LASSERT(!IS_ERR(dentry));
57         sbi = ll_s2sbi(dentry->d_sb);
58         
59         lwi = LWI_TIMEOUT(timeout * HZ, NULL, NULL);
60         for (; !d_mountpoint(dentry) && tries > 0; tries--)
61                 l_wait_event(sbi->ll_gns_waitq, d_mountpoint(dentry), &lwi);
62
63         if (d_mountpoint(dentry)) {
64                 spin_lock(&sbi->ll_gns_lock);
65                 sbi->ll_gns_state = LL_GNS_FINISHED;
66                 spin_unlock(&sbi->ll_gns_lock);
67                 RETURN(0);
68         }
69         RETURN(-ETIME);
70 }
71
72 /*
73  * tries to mount the mount object under passed @dentry. In the case of success
74  * @dentry will become mount point and 0 will be returned. Error code will be
75  * returned otherwise.
76  */
77 int
78 ll_gns_mount_object(struct dentry *dentry, struct vfsmount *mnt)
79 {
80         struct ll_dentry_data *lld = dentry->d_fsdata;
81         char *path, *pathpage, *datapage, *argv[4];
82         struct file *mntinfo_fd = NULL;
83         int cleanup_phase = 0, rc = 0;
84         struct ll_sb_info *sbi;
85         struct dentry *dchild;
86         ENTRY;
87
88         if (mnt == NULL) {
89                 CERROR("suid directory found, but no "
90                        "vfsmount available.\n");
91                 RETURN(-EINVAL);
92         }
93
94         CDEBUG(D_INODE, "mounting dentry %p\n", dentry);
95
96         LASSERT(dentry->d_inode != NULL);
97         LASSERT(S_ISDIR(dentry->d_inode->i_mode));
98         LASSERT(lld != NULL);
99         
100         sbi = ll_i2sbi(dentry->d_inode);
101         LASSERT(sbi != NULL);
102
103         /* 
104          * another thead is in progress or just finished mounting the
105          * dentry. Handling that.
106          */
107         spin_lock(&sbi->ll_gns_lock);
108         if (sbi->ll_gns_state == LL_GNS_MOUNTING ||
109             sbi->ll_gns_state == LL_GNS_FINISHED) {
110                 /* 
111                  * check if another control thread is trying to mount some GNS
112                  * dentry too. Letting it know that we busy and make
113                  * ll_lookup_it() ti restart syscall and try again later.
114                  */
115                 spin_unlock(&sbi->ll_gns_lock);
116                 CDEBUG(D_INODE, "GNS is in progress now, throwing "
117                        "-ERESTARTSYS to restart syscall and let "
118                        "it finish.\n");
119                 RETURN(-EBUSY);
120         }
121         LASSERT(sbi->ll_gns_state == LL_GNS_IDLE);
122
123         /* mounting started */
124         sbi->ll_gns_state = LL_GNS_MOUNTING;
125         spin_unlock(&sbi->ll_gns_lock);
126
127         /* we need to build an absolute pathname to pass to mount */
128         pathpage = (char *)__get_free_page(GFP_KERNEL);
129         if (!pathpage)
130                 GOTO(cleanup, rc = -ENOMEM);
131         cleanup_phase = 1;
132
133         /* getting @dentry path stored in @pathpage. */
134         path = d_path(dentry, mnt, pathpage, PAGE_SIZE);
135         if (IS_ERR(path)) {
136                 CERROR("can't build mount object path, err %d\n",
137                        (int)PTR_ERR(dchild));
138                 GOTO(cleanup, rc = PTR_ERR(dchild));
139         }
140
141         /* synchronizing with possible /proc/fs/...write */
142         down(&sbi->ll_gns_sem);
143         
144         /* 
145          * mount object name is taken from sbi, where it is set in mount time or
146          * via /proc/fs... tunable. It may be ".mntinfo" or so.
147          */
148
149         /* 
150          * recursive lookup with trying to mount SUID bit marked directories on
151          * the way is not possible here, as lookup_one_len() does not pass nd to
152          * ->lookup() and this is checked in ll_lookup_it(). So, do not handle
153          * possible -EBUSY here.
154          */
155         dchild = ll_lookup_one_len(sbi->ll_gns_oname, dentry,
156                                    strlen(sbi->ll_gns_oname));
157         up(&sbi->ll_gns_sem);
158
159         cleanup_phase = 2;
160         
161         if (IS_ERR(dchild)) {
162                 rc = PTR_ERR(dchild);
163                 CERROR("can't find mount object %*s/%*s err = %d.\n",
164                        (int)dentry->d_name.len, dentry->d_name.name,
165                        strlen(sbi->ll_gns_oname), sbi->ll_gns_oname,
166                        rc);
167                 GOTO(cleanup, rc);
168         }
169
170         /* mount object is not found */
171         if (!dchild->d_inode)
172                 GOTO(cleanup, rc = -ENOENT);
173
174         /* check if found child is regular file */
175         if (!S_ISREG(dchild->d_inode->i_mode))
176                 GOTO(cleanup, rc = -EOPNOTSUPP);
177
178         mntget(mnt);
179
180         /* ok, mount object if found, opening it. */
181         mntinfo_fd = dentry_open(dchild, mnt, 0);
182         if (IS_ERR(mntinfo_fd)) {
183                 CERROR("can't open mount object %*s/%*s err = %d.\n",
184                        (int)dentry->d_name.len, dentry->d_name.name,
185                        strlen(sbi->ll_gns_oname), sbi->ll_gns_oname,
186                        (int)PTR_ERR(mntinfo_fd));
187                 mntput(mnt);
188                 GOTO(cleanup, rc = PTR_ERR(mntinfo_fd));
189         }
190         cleanup_phase = 3;
191
192         if (mntinfo_fd->f_dentry->d_inode->i_size > PAGE_SIZE - 1) {
193                 CERROR("mount object %*s/%*s is too big (%Ld)\n",
194                        (int)dentry->d_name.len, dentry->d_name.name,
195                        strlen(sbi->ll_gns_oname), sbi->ll_gns_oname,
196                        mntinfo_fd->f_dentry->d_inode->i_size);
197                 GOTO(cleanup, rc = -EFBIG);
198         }
199
200         datapage = (char *)__get_free_page(GFP_KERNEL);
201         if (!datapage)
202                 GOTO(cleanup, rc = -ENOMEM);
203
204         cleanup_phase = 4;
205         
206         /* read data from mount object. */
207         rc = kernel_read(mntinfo_fd, 0, datapage, PAGE_SIZE - 1);
208         if (rc < 0) {
209                 CERROR("can't read mount object %*s/%*s data, err %d\n",
210                        (int)dentry->d_name.len, dentry->d_name.name,
211                        strlen(sbi->ll_gns_oname), sbi->ll_gns_oname,
212                        rc);
213                 GOTO(cleanup, rc);
214         }
215
216         /* no data in mount object? */
217         if (rc == 0) {
218                 CERROR("mount object %*s/%*s is empty?\n",
219                        (int)dentry->d_name.len, dentry->d_name.name,
220                        strlen(sbi->ll_gns_oname), sbi->ll_gns_oname);
221                 GOTO(cleanup, rc);
222         }
223
224         datapage[rc] = '\0';
225         fput(mntinfo_fd);
226         mntinfo_fd = NULL;
227         dchild = NULL;
228
229         /* synchronizing with possible /proc/fs/...write */
230         down(&sbi->ll_gns_sem);
231
232         /*
233          * upcall is initialized in mount time or via /proc/fs/... tuneable and
234          * may be /usr/lib/lustre/gns-upcall.sh
235          */
236         argv[0] = sbi->ll_gns_upcall;
237         argv[1] = datapage;
238         argv[2] = path;
239         argv[3] = NULL;
240         
241         up(&sbi->ll_gns_sem);
242
243         rc = USERMODEHELPER(argv[0], argv, NULL);
244         if (rc) {
245                 CERROR("failed to call GNS upcall %s, err = %d\n",
246                        sbi->ll_gns_upcall, rc);
247                 GOTO(cleanup, rc);
248         }
249
250         /*
251          * wait for mount completion. This is actually not needed, because
252          * USERMODEHELPER() returns only when usermode process finishes. But we
253          * doing this just for case USERMODEHELPER() semantics will be changed
254          * or usermode upcall program will start mounting in backgound and
255          * return instantly. --umka
256          */
257         rc = ll_gns_wait_for_mount(dentry, 1, GNS_WAIT_ATTEMPTS);
258         complete_all(&sbi->ll_gns_mount_finished);
259         if (rc == 0) {
260                 struct dentry *rdentry;
261                 struct vfsmount *rmnt;
262                 
263                 /* mount is successful */
264                 LASSERT(sbi->ll_gns_state == LL_GNS_FINISHED);
265
266                 rmnt = mntget(mnt);
267                 rdentry = dget(dentry);
268                 
269                 if (follow_down(&rmnt, &rdentry)) {
270                         /* 
271                          * registering new mount in GNS mounts list and thus
272                          * make it accessible from GNS control thread.
273                          */
274                         spin_lock(&dcache_lock);
275                         LASSERT(list_empty(&rmnt->mnt_lustre_list));
276                         list_add_tail(&rmnt->mnt_lustre_list,
277                                       &sbi->ll_mnt_list);
278                         spin_unlock(&dcache_lock);
279                         rmnt->mnt_last_used = jiffies;
280                         mntput(rmnt);
281                         dput(rdentry);
282                 } else {
283                         mntput(mnt);
284                         dput(dentry);
285                 }
286         } else {
287                 CERROR("usermode upcall %s failed to mount %s, err %d\n",
288                        sbi->ll_gns_upcall, path, rc);
289         }
290                 
291         EXIT;
292 cleanup:
293         switch (cleanup_phase) {
294         case 4:
295                 free_page((unsigned long)datapage);
296         case 3:
297                 if (mntinfo_fd != NULL)
298                         fput(mntinfo_fd);
299         case 2:
300                 if (dchild != NULL)
301                         dput(dchild);
302         case 1:
303                 free_page((unsigned long)pathpage);
304                 
305                 /* 
306                  * waking up all waiters after gns state is set to
307                  * LL_GNS_MOUNTING
308                  */
309                 complete_all(&sbi->ll_gns_mount_finished);
310         case 0:
311                 spin_lock(&sbi->ll_gns_lock);
312                 sbi->ll_gns_state = LL_GNS_IDLE;
313                 spin_unlock(&sbi->ll_gns_lock);
314         }
315         return rc;
316 }
317
318 /* tries to umount passed @mnt. */
319 int ll_gns_umount_object(struct vfsmount *mnt)
320 {
321         int rc = 0;
322         ENTRY;
323         
324         CDEBUG(D_INODE, "unmounting mnt %p\n", mnt);
325         rc = do_umount(mnt, 0);
326         if (rc) {
327                 CDEBUG(D_INODE, "can't umount 0x%p, err = %d\n",
328                        mnt, rc);
329         }
330         
331         RETURN(rc);
332 }
333
334 int ll_gns_check_mounts(struct ll_sb_info *sbi, int flags)
335 {
336         struct list_head check_list = LIST_HEAD_INIT(check_list);
337         struct vfsmount *mnt;
338         unsigned long pass;
339         ENTRY;
340
341         spin_lock(&dcache_lock);
342         list_splice_init(&sbi->ll_mnt_list, &check_list);
343
344         /*
345          * walk the list in reverse order, and put them on the front of the sbi
346          * list each iteration; this avoids list-ordering problems if we race
347          * with another gns-mounting thread.
348          */
349         while (!list_empty(&check_list)) {
350                 mnt = list_entry(check_list.prev,
351                                  struct vfsmount,
352                                  mnt_lustre_list);
353
354                 mntget(mnt);
355
356                 list_del_init(&mnt->mnt_lustre_list);
357
358                 list_add(&mnt->mnt_lustre_list,
359                          &sbi->ll_mnt_list);
360
361                 /* check for timeout if needed */
362                 pass = jiffies - mnt->mnt_last_used;
363                 
364                 if (flags == LL_GNS_CHECK &&
365                     pass < sbi->ll_gns_timeout * HZ)
366                 {
367                         mntput(mnt);
368                         continue;
369                 }
370                 spin_unlock(&dcache_lock);
371
372                 /* umounting @mnt */
373                 ll_gns_umount_object(mnt);
374
375                 mntput(mnt);
376                 spin_lock(&dcache_lock);
377         }
378         spin_unlock(&dcache_lock);
379         RETURN(0);
380 }
381
382 /*
383  * GNS timer callback function. It restarts gns timer and wakes up GNS control
384  * thread to process mounts list.
385  */
386 void ll_gns_timer_callback(unsigned long data)
387 {
388         struct ll_sb_info *sbi = (void *)data;
389         ENTRY;
390
391         spin_lock(&gns_lock);
392         if (list_empty(&sbi->ll_gns_sbi_head))
393                 list_add(&sbi->ll_gns_sbi_head, &gns_sbi_list);
394         spin_unlock(&gns_lock);
395         
396         wake_up(&gns_thread.t_ctl_waitq);
397         mod_timer(&sbi->ll_gns_timer,
398                   jiffies + sbi->ll_gns_tick * HZ);
399 }
400
401 /* this function checks if something new happened to exist in gns list. */
402 static int inline ll_gns_check_event(void)
403 {
404         int rc;
405         
406         spin_lock(&gns_lock);
407         rc = !list_empty(&gns_sbi_list);
408         spin_unlock(&gns_lock);
409
410         return rc;
411 }
412
413 /* should we stop GNS control thread? */
414 static int inline ll_gns_check_stop(void)
415 {
416         mb();
417         return (gns_thread.t_flags & SVC_STOPPING) ? 1 : 0;
418 }
419
420 /* GNS control thread function. */
421 static int ll_gns_thread_main(void *arg)
422 {
423         struct ll_gns_ctl *ctl = arg;
424         unsigned long flags;
425         ENTRY;
426
427         {
428                 char name[sizeof(current->comm)];
429                 snprintf(name, sizeof(name) - 1, "ll_gns");
430                 kportal_daemonize(name);
431         }
432         
433         SIGNAL_MASK_LOCK(current, flags);
434         sigfillset(&current->blocked);
435         RECALC_SIGPENDING;
436         SIGNAL_MASK_UNLOCK(current, flags);
437
438         /*
439          * letting starting function know, that we are ready and control may be
440          * returned.
441          */
442         gns_thread.t_flags = SVC_RUNNING;
443         complete(&ctl->gc_starting);
444
445         while (!ll_gns_check_stop()) {
446                 struct l_wait_info lwi = { 0 };
447
448                 l_wait_event(gns_thread.t_ctl_waitq,
449                              (ll_gns_check_event() ||
450                               ll_gns_check_stop()), &lwi);
451                 
452                 spin_lock(&gns_lock);
453                 while (!list_empty(&gns_sbi_list)) {
454                         struct ll_sb_info *sbi;
455
456                         sbi = list_entry(gns_sbi_list.prev,
457                                          struct ll_sb_info,
458                                          ll_gns_sbi_head);
459                         
460                         list_del_init(&sbi->ll_gns_sbi_head);
461                         spin_unlock(&gns_lock);
462                         ll_gns_check_mounts(sbi, LL_GNS_CHECK);
463                         spin_lock(&gns_lock);
464                 }
465                 spin_unlock(&gns_lock);
466         }
467
468         EXIT;
469         gns_thread.t_flags = SVC_STOPPED;
470
471         /* this is SMP-safe way to finish thread. */
472         complete_and_exit(&ctl->gc_finishing, 0);
473 }
474
475 void ll_gns_add_timer(struct ll_sb_info *sbi)
476 {
477         mod_timer(&sbi->ll_gns_timer,
478                   jiffies + sbi->ll_gns_tick * HZ);
479 }
480
481 void ll_gns_del_timer(struct ll_sb_info *sbi)
482 {
483         del_timer(&sbi->ll_gns_timer);
484 }
485
486 /*
487  * starts GNS control thread and waits for a signal it is up and work may be
488  * continued.
489  */
490 int ll_gns_start_thread(void)
491 {
492         int rc;
493         ENTRY;
494
495         LASSERT(gns_thread.t_flags == 0);
496         init_completion(&gns_ctl.gc_starting);
497         init_completion(&gns_ctl.gc_finishing);
498         init_waitqueue_head(&gns_thread.t_ctl_waitq);
499         
500         rc = kernel_thread(ll_gns_thread_main, &gns_ctl,
501                            (CLONE_VM | CLONE_FILES));
502         if (rc < 0) {
503                 CERROR("cannot start GNS control thread, "
504                        "err = %d\n", rc);
505                 RETURN(rc);
506         }
507         wait_for_completion(&gns_ctl.gc_starting);
508         LASSERT(gns_thread.t_flags == SVC_RUNNING);
509         RETURN(0);
510 }
511
512 /* stops GNS control thread and waits its actual stop. */
513 void ll_gns_stop_thread(void)
514 {
515         ENTRY;
516         gns_thread.t_flags = SVC_STOPPING;
517         wake_up(&gns_thread.t_ctl_waitq);
518         wait_for_completion(&gns_ctl.gc_finishing);
519         LASSERT(gns_thread.t_flags == SVC_STOPPED);
520         gns_thread.t_flags = 0;
521         EXIT;
522 }