1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004, 2005 Cluster File Systems, Inc.
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>
11 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
27 #define DEBUG_SUBSYSTEM S_LLITE
30 #include <linux/version.h>
31 #include <asm/uaccess.h>
32 #include <linux/file.h>
33 #include <linux/kmod.h>
35 #include <linux/lustre_lite.h>
36 #include "llite_internal.h"
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;
44 * waits until passed dentry gets mountpoint or timeout and attempts are
45 * exhausted. Returns 1 if dentry became mountpoint and 0 otherwise.
48 ll_gns_wait_for_mount(struct dentry *dentry,
49 int timeout, int tries)
51 struct l_wait_info lwi;
52 struct ll_sb_info *sbi;
55 LASSERT(dentry != NULL);
56 LASSERT(!IS_ERR(dentry));
57 sbi = ll_s2sbi(dentry->d_sb);
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);
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);
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
78 ll_gns_mount_object(struct dentry *dentry, struct vfsmount *mnt)
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;
89 CERROR("suid directory found, but no "
90 "vfsmount available.\n");
94 CDEBUG(D_INODE, "mounting dentry %p\n", dentry);
96 LASSERT(dentry->d_inode != NULL);
97 LASSERT(S_ISDIR(dentry->d_inode->i_mode));
100 sbi = ll_i2sbi(dentry->d_inode);
101 LASSERT(sbi != NULL);
104 * another thead is in progress or just finished mounting the
105 * dentry. Handling that.
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 spin_unlock(&sbi->ll_gns_lock);
111 CDEBUG(D_INODE, "GNS is in progress now, throwing "
112 "-ERESTARTSYS to restart syscall and let "
114 RETURN(-ERESTARTSYS);
116 LASSERT(sbi->ll_gns_state == LL_GNS_IDLE);
118 spin_lock(&dentry->d_lock);
119 dentry->d_flags |= DCACHE_GNS_MOUNTING;
120 spin_unlock(&dentry->d_lock);
122 /* mounting started */
123 sbi->ll_gns_state = LL_GNS_MOUNTING;
124 spin_unlock(&sbi->ll_gns_lock);
126 /* we need to build an absolute pathname to pass to mount */
127 pathpage = (char *)__get_free_page(GFP_KERNEL);
129 GOTO(cleanup, rc = -ENOMEM);
132 /* getting @dentry path stored in @pathpage. */
133 path = d_path(dentry, mnt, pathpage, PAGE_SIZE);
135 CERROR("can't build mount object path, err %d\n",
136 (int)PTR_ERR(dchild));
137 GOTO(cleanup, rc = PTR_ERR(dchild));
140 /* synchronizing with possible /proc/fs/...write */
141 down(&sbi->ll_gns_sem);
144 * mount object name is taken from sbi, where it is set in mount time or
145 * via /proc/fs... tunable. It may be ".mntinfo" or so.
149 * FIXME: lookup_one_len() requires dentry->d_inode->i_sem to be locked,
150 * but we can't use ll_lookup_one_len() as this function is called from
151 * different contol paths and some of them take dentry->d_inode->i_sem
154 dchild = lookup_one_len(sbi->ll_gns_oname, dentry,
155 strlen(sbi->ll_gns_oname));
156 up(&sbi->ll_gns_sem);
160 if (IS_ERR(dchild)) {
161 rc = PTR_ERR(dchild);
163 if (rc == -ERESTARTSYS) {
164 CDEBUG(D_INODE, "possible endless loop is detected "
165 "due to mount object is directory marked by "
167 GOTO(cleanup, rc = -ELOOP);
170 CERROR("can't find mount object %*s/%*s err = %d.\n",
171 (int)dentry->d_name.len, dentry->d_name.name,
172 strlen(sbi->ll_gns_oname), sbi->ll_gns_oname,
177 /* mount object is not found */
178 if (!dchild->d_inode)
179 GOTO(cleanup, rc = -ENOENT);
181 /* check if found child is regular file */
182 if (!S_ISREG(dchild->d_inode->i_mode))
183 GOTO(cleanup, rc = -EOPNOTSUPP);
187 /* ok, mount object if found, opening it. */
188 mntinfo_fd = dentry_open(dchild, mnt, 0);
189 if (IS_ERR(mntinfo_fd)) {
190 CERROR("can't open mount object %*s/%*s err = %d.\n",
191 (int)dentry->d_name.len, dentry->d_name.name,
192 strlen(sbi->ll_gns_oname), sbi->ll_gns_oname,
193 (int)PTR_ERR(mntinfo_fd));
195 GOTO(cleanup, rc = PTR_ERR(mntinfo_fd));
199 if (mntinfo_fd->f_dentry->d_inode->i_size > PAGE_SIZE) {
200 CERROR("mount object %*s/%*s is too big (%Ld)\n",
201 (int)dentry->d_name.len, dentry->d_name.name,
202 strlen(sbi->ll_gns_oname), sbi->ll_gns_oname,
203 mntinfo_fd->f_dentry->d_inode->i_size);
204 GOTO(cleanup, rc = -EFBIG);
207 datapage = (char *)__get_free_page(GFP_KERNEL);
209 GOTO(cleanup, rc = -ENOMEM);
213 /* read data from mount object. */
214 rc = kernel_read(mntinfo_fd, 0, datapage, PAGE_SIZE);
216 CERROR("can't read mount object %*s/%*s data, err %d\n",
217 (int)dentry->d_name.len, dentry->d_name.name,
218 strlen(sbi->ll_gns_oname), sbi->ll_gns_oname,
223 datapage[PAGE_SIZE - 1] = '\0';
229 /* synchronizing with possible /proc/fs/...write */
230 down(&sbi->ll_gns_sem);
233 * upcall is initialized in mount time or via /proc/fs/... tuneable and
234 * may be /usr/lib/lustre/gns-upcall.sh
236 argv[0] = sbi->ll_gns_upcall;
241 up(&sbi->ll_gns_sem);
243 rc = USERMODEHELPER(argv[0], argv, NULL);
245 CERROR("failed to call GNS upcall %s, err = %d\n",
246 sbi->ll_gns_upcall, rc);
251 * wait for mount completion. This is actually not need, 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
257 rc = ll_gns_wait_for_mount(dentry, 1, GNS_WAIT_ATTEMPTS);
258 complete_all(&sbi->ll_gns_mount_finished);
260 struct dentry *rdentry;
261 struct vfsmount *rmnt;
263 /* mount is successful */
264 LASSERT(sbi->ll_gns_state == LL_GNS_FINISHED);
267 rdentry = dget(dentry);
269 if (follow_down(&rmnt, &rdentry)) {
271 * registering new mount in GNS mounts list and thus
272 * make it accessible from GNS control thread.
274 spin_lock(&dcache_lock);
275 LASSERT(list_empty(&rmnt->mnt_lustre_list));
276 list_add_tail(&rmnt->mnt_lustre_list,
278 spin_unlock(&dcache_lock);
279 rmnt->mnt_last_used = jiffies;
286 spin_lock(&dentry->d_lock);
287 dentry->d_flags &= ~DCACHE_GNS_PENDING;
288 spin_unlock(&dentry->d_lock);
290 CERROR("usermode upcall %s failed to mount %s, err %d\n",
291 sbi->ll_gns_upcall, path, rc);
296 switch (cleanup_phase) {
298 free_page((unsigned long)datapage);
300 if (mntinfo_fd != NULL)
306 free_page((unsigned long)pathpage);
309 * waking up all waiters after gns state is set to
312 complete_all(&sbi->ll_gns_mount_finished);
314 spin_lock(&sbi->ll_gns_lock);
315 sbi->ll_gns_state = LL_GNS_IDLE;
316 spin_unlock(&sbi->ll_gns_lock);
318 spin_lock(&dentry->d_lock);
319 dentry->d_flags &= ~DCACHE_GNS_MOUNTING;
320 spin_unlock(&dentry->d_lock);
325 /* tries to umount passed @mnt. */
326 int ll_gns_umount_object(struct vfsmount *mnt)
331 CDEBUG(D_INODE, "unmounting mnt %p\n", mnt);
332 rc = do_umount(mnt, 0);
334 CDEBUG(D_INODE, "can't umount 0x%p, err = %d\n",
341 int ll_gns_check_mounts(struct ll_sb_info *sbi, int flags)
343 struct list_head check_list = LIST_HEAD_INIT(check_list);
344 struct vfsmount *mnt;
348 spin_lock(&dcache_lock);
349 list_splice_init(&sbi->ll_mnt_list, &check_list);
352 * walk the list in reverse order, and put them on the front of the sbi
353 * list each iteration; this avoids list-ordering problems if we race
354 * with another gns-mounting thread.
356 while (!list_empty(&check_list)) {
357 mnt = list_entry(check_list.prev,
363 list_del_init(&mnt->mnt_lustre_list);
365 list_add(&mnt->mnt_lustre_list,
368 /* check for timeout if needed */
369 pass = jiffies - mnt->mnt_last_used;
371 if (flags == LL_GNS_CHECK &&
372 pass < sbi->ll_gns_timeout * HZ)
377 spin_unlock(&dcache_lock);
380 ll_gns_umount_object(mnt);
383 spin_lock(&dcache_lock);
385 spin_unlock(&dcache_lock);
390 * GNS timer callback function. It restarts gns timer and wakes up GNS control
391 * thread to process mounts list.
393 void ll_gns_timer_callback(unsigned long data)
395 struct ll_sb_info *sbi = (void *)data;
398 spin_lock(&gns_lock);
399 if (list_empty(&sbi->ll_gns_sbi_head))
400 list_add(&sbi->ll_gns_sbi_head, &gns_sbi_list);
401 spin_unlock(&gns_lock);
403 wake_up(&gns_thread.t_ctl_waitq);
404 mod_timer(&sbi->ll_gns_timer,
405 jiffies + sbi->ll_gns_tick * HZ);
408 /* this function checks if something new happened to exist in gns list. */
409 static int inline ll_gns_check_event(void)
413 spin_lock(&gns_lock);
414 rc = !list_empty(&gns_sbi_list);
415 spin_unlock(&gns_lock);
420 /* should we stop GNS control thread? */
421 static int inline ll_gns_check_stop(void)
424 return (gns_thread.t_flags & SVC_STOPPING) ? 1 : 0;
427 /* GNS control thread function. */
428 static int ll_gns_thread_main(void *arg)
430 struct ll_gns_ctl *ctl = arg;
435 char name[sizeof(current->comm)];
436 snprintf(name, sizeof(name) - 1, "ll_gns");
437 kportal_daemonize(name);
440 SIGNAL_MASK_LOCK(current, flags);
441 sigfillset(¤t->blocked);
443 SIGNAL_MASK_UNLOCK(current, flags);
446 * letting starting function know, that we are ready and control may be
449 gns_thread.t_flags = SVC_RUNNING;
450 complete(&ctl->gc_starting);
452 while (!ll_gns_check_stop()) {
453 struct l_wait_info lwi = { 0 };
455 l_wait_event(gns_thread.t_ctl_waitq,
456 (ll_gns_check_event() ||
457 ll_gns_check_stop()), &lwi);
459 spin_lock(&gns_lock);
460 while (!list_empty(&gns_sbi_list)) {
461 struct ll_sb_info *sbi;
463 sbi = list_entry(gns_sbi_list.prev,
467 list_del_init(&sbi->ll_gns_sbi_head);
468 spin_unlock(&gns_lock);
469 ll_gns_check_mounts(sbi, LL_GNS_CHECK);
470 spin_lock(&gns_lock);
472 spin_unlock(&gns_lock);
476 gns_thread.t_flags = SVC_STOPPED;
478 /* this is SMP-safe way to finish thread. */
479 complete_and_exit(&ctl->gc_finishing, 0);
482 void ll_gns_add_timer(struct ll_sb_info *sbi)
484 mod_timer(&sbi->ll_gns_timer,
485 jiffies + sbi->ll_gns_tick * HZ);
488 void ll_gns_del_timer(struct ll_sb_info *sbi)
490 del_timer(&sbi->ll_gns_timer);
494 * starts GNS control thread and waits for a signal it is up and work may be
497 int ll_gns_start_thread(void)
502 LASSERT(gns_thread.t_flags == 0);
503 init_completion(&gns_ctl.gc_starting);
504 init_completion(&gns_ctl.gc_finishing);
505 init_waitqueue_head(&gns_thread.t_ctl_waitq);
507 rc = kernel_thread(ll_gns_thread_main, &gns_ctl,
508 (CLONE_VM | CLONE_FILES));
510 CERROR("cannot start GNS control thread, "
514 wait_for_completion(&gns_ctl.gc_starting);
515 LASSERT(gns_thread.t_flags == SVC_RUNNING);
519 /* stops GNS control thread and waits its actual stop. */
520 void ll_gns_stop_thread(void)
523 gns_thread.t_flags = SVC_STOPPING;
524 wake_up(&gns_thread.t_ctl_waitq);
525 wait_for_completion(&gns_ctl.gc_finishing);
526 LASSERT(gns_thread.t_flags == SVC_STOPPED);
527 gns_thread.t_flags = 0;