[PATCH] handling of slave mounts
[safe/jmp/linux-2.6] / fs / pnode.c
1 /*
2  *  linux/fs/pnode.c
3  *
4  * (C) Copyright IBM Corporation 2005.
5  *      Released under GPL v2.
6  *      Author : Ram Pai (linuxram@us.ibm.com)
7  *
8  */
9 #include <linux/namespace.h>
10 #include <linux/mount.h>
11 #include <linux/fs.h>
12 #include "pnode.h"
13
14 /* return the next shared peer mount of @p */
15 static inline struct vfsmount *next_peer(struct vfsmount *p)
16 {
17         return list_entry(p->mnt_share.next, struct vfsmount, mnt_share);
18 }
19
20 static inline struct vfsmount *first_slave(struct vfsmount *p)
21 {
22         return list_entry(p->mnt_slave_list.next, struct vfsmount, mnt_slave);
23 }
24
25 static inline struct vfsmount *next_slave(struct vfsmount *p)
26 {
27         return list_entry(p->mnt_slave.next, struct vfsmount, mnt_slave);
28 }
29
30 static int do_make_slave(struct vfsmount *mnt)
31 {
32         struct vfsmount *peer_mnt = mnt, *master = mnt->mnt_master;
33         struct vfsmount *slave_mnt;
34
35         /*
36          * slave 'mnt' to a peer mount that has the
37          * same root dentry. If none is available than
38          * slave it to anything that is available.
39          */
40         while ((peer_mnt = next_peer(peer_mnt)) != mnt &&
41                peer_mnt->mnt_root != mnt->mnt_root) ;
42
43         if (peer_mnt == mnt) {
44                 peer_mnt = next_peer(mnt);
45                 if (peer_mnt == mnt)
46                         peer_mnt = NULL;
47         }
48         list_del_init(&mnt->mnt_share);
49
50         if (peer_mnt)
51                 master = peer_mnt;
52
53         if (master) {
54                 list_for_each_entry(slave_mnt, &mnt->mnt_slave_list, mnt_slave)
55                         slave_mnt->mnt_master = master;
56                 list_del(&mnt->mnt_slave);
57                 list_add(&mnt->mnt_slave, &master->mnt_slave_list);
58                 list_splice(&mnt->mnt_slave_list, master->mnt_slave_list.prev);
59                 INIT_LIST_HEAD(&mnt->mnt_slave_list);
60         } else {
61                 struct list_head *p = &mnt->mnt_slave_list;
62                 while (!list_empty(p)) {
63                         slave_mnt = list_entry(p->next,
64                                         struct vfsmount, mnt_slave);
65                         list_del_init(&slave_mnt->mnt_slave);
66                         slave_mnt->mnt_master = NULL;
67                 }
68         }
69         mnt->mnt_master = master;
70         CLEAR_MNT_SHARED(mnt);
71         INIT_LIST_HEAD(&mnt->mnt_slave_list);
72         return 0;
73 }
74
75 void change_mnt_propagation(struct vfsmount *mnt, int type)
76 {
77         if (type == MS_SHARED) {
78                 set_mnt_shared(mnt);
79                 return;
80         }
81         do_make_slave(mnt);
82         if (type != MS_SLAVE) {
83                 list_del_init(&mnt->mnt_slave);
84                 mnt->mnt_master = NULL;
85         }
86 }
87
88 /*
89  * get the next mount in the propagation tree.
90  * @m: the mount seen last
91  * @origin: the original mount from where the tree walk initiated
92  */
93 static struct vfsmount *propagation_next(struct vfsmount *m,
94                                          struct vfsmount *origin)
95 {
96         /* are there any slaves of this mount? */
97         if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
98                 return first_slave(m);
99
100         while (1) {
101                 struct vfsmount *next;
102                 struct vfsmount *master = m->mnt_master;
103
104                 if ( master == origin->mnt_master ) {
105                         next = next_peer(m);
106                         return ((next == origin) ? NULL : next);
107                 } else if (m->mnt_slave.next != &master->mnt_slave_list)
108                         return next_slave(m);
109
110                 /* back at master */
111                 m = master;
112         }
113 }
114
115 /*
116  * return the source mount to be used for cloning
117  *
118  * @dest        the current destination mount
119  * @last_dest   the last seen destination mount
120  * @last_src    the last seen source mount
121  * @type        return CL_SLAVE if the new mount has to be
122  *              cloned as a slave.
123  */
124 static struct vfsmount *get_source(struct vfsmount *dest,
125                                         struct vfsmount *last_dest,
126                                         struct vfsmount *last_src,
127                                         int *type)
128 {
129         struct vfsmount *p_last_src = NULL;
130         struct vfsmount *p_last_dest = NULL;
131         *type = CL_PROPAGATION;;
132
133         if (IS_MNT_SHARED(dest))
134                 *type |= CL_MAKE_SHARED;
135
136         while (last_dest != dest->mnt_master) {
137                 p_last_dest = last_dest;
138                 p_last_src = last_src;
139                 last_dest = last_dest->mnt_master;
140                 last_src = last_src->mnt_master;
141         }
142
143         if (p_last_dest) {
144                 do {
145                         p_last_dest = next_peer(p_last_dest);
146                 } while (IS_MNT_NEW(p_last_dest));
147         }
148
149         if (dest != p_last_dest) {
150                 *type |= CL_SLAVE;
151                 return last_src;
152         } else
153                 return p_last_src;
154 }
155
156 /*
157  * mount 'source_mnt' under the destination 'dest_mnt' at
158  * dentry 'dest_dentry'. And propagate that mount to
159  * all the peer and slave mounts of 'dest_mnt'.
160  * Link all the new mounts into a propagation tree headed at
161  * source_mnt. Also link all the new mounts using ->mnt_list
162  * headed at source_mnt's ->mnt_list
163  *
164  * @dest_mnt: destination mount.
165  * @dest_dentry: destination dentry.
166  * @source_mnt: source mount.
167  * @tree_list : list of heads of trees to be attached.
168  */
169 int propagate_mnt(struct vfsmount *dest_mnt, struct dentry *dest_dentry,
170                     struct vfsmount *source_mnt, struct list_head *tree_list)
171 {
172         struct vfsmount *m, *child;
173         int ret = 0;
174         struct vfsmount *prev_dest_mnt = dest_mnt;
175         struct vfsmount *prev_src_mnt  = source_mnt;
176         LIST_HEAD(tmp_list);
177         LIST_HEAD(umount_list);
178
179         for (m = propagation_next(dest_mnt, dest_mnt); m;
180                         m = propagation_next(m, dest_mnt)) {
181                 int type;
182                 struct vfsmount *source;
183
184                 if (IS_MNT_NEW(m))
185                         continue;
186
187                 source =  get_source(m, prev_dest_mnt, prev_src_mnt, &type);
188
189                 if (!(child = copy_tree(source, source->mnt_root, type))) {
190                         ret = -ENOMEM;
191                         list_splice(tree_list, tmp_list.prev);
192                         goto out;
193                 }
194
195                 if (is_subdir(dest_dentry, m->mnt_root)) {
196                         mnt_set_mountpoint(m, dest_dentry, child);
197                         list_add_tail(&child->mnt_hash, tree_list);
198                 } else {
199                         /*
200                          * This can happen if the parent mount was bind mounted
201                          * on some subdirectory of a shared/slave mount.
202                          */
203                         list_add_tail(&child->mnt_hash, &tmp_list);
204                 }
205                 prev_dest_mnt = m;
206                 prev_src_mnt  = child;
207         }
208 out:
209         spin_lock(&vfsmount_lock);
210         while (!list_empty(&tmp_list)) {
211                 child = list_entry(tmp_list.next, struct vfsmount, mnt_hash);
212                 list_del_init(&child->mnt_hash);
213                 umount_tree(child, 0, &umount_list);
214         }
215         spin_unlock(&vfsmount_lock);
216         release_mounts(&umount_list);
217         return ret;
218 }
219
220 /*
221  * return true if the refcount is greater than count
222  */
223 static inline int do_refcount_check(struct vfsmount *mnt, int count)
224 {
225         int mycount = atomic_read(&mnt->mnt_count);
226         return (mycount > count);
227 }
228
229 /*
230  * check if the mount 'mnt' can be unmounted successfully.
231  * @mnt: the mount to be checked for unmount
232  * NOTE: unmounting 'mnt' would naturally propagate to all
233  * other mounts its parent propagates to.
234  * Check if any of these mounts that **do not have submounts**
235  * have more references than 'refcnt'. If so return busy.
236  */
237 int propagate_mount_busy(struct vfsmount *mnt, int refcnt)
238 {
239         struct vfsmount *m, *child;
240         struct vfsmount *parent = mnt->mnt_parent;
241         int ret = 0;
242
243         if (mnt == parent)
244                 return do_refcount_check(mnt, refcnt);
245
246         /*
247          * quickly check if the current mount can be unmounted.
248          * If not, we don't have to go checking for all other
249          * mounts
250          */
251         if (!list_empty(&mnt->mnt_mounts) || do_refcount_check(mnt, refcnt))
252                 return 1;
253
254         for (m = propagation_next(parent, parent); m;
255                         m = propagation_next(m, parent)) {
256                 child = __lookup_mnt(m, mnt->mnt_mountpoint, 0);
257                 if (child && list_empty(&child->mnt_mounts) &&
258                     (ret = do_refcount_check(child, 1)))
259                         break;
260         }
261         return ret;
262 }
263
264 /*
265  * NOTE: unmounting 'mnt' naturally propagates to all other mounts its
266  * parent propagates to.
267  */
268 static void __propagate_umount(struct vfsmount *mnt)
269 {
270         struct vfsmount *parent = mnt->mnt_parent;
271         struct vfsmount *m;
272
273         BUG_ON(parent == mnt);
274
275         for (m = propagation_next(parent, parent); m;
276                         m = propagation_next(m, parent)) {
277
278                 struct vfsmount *child = __lookup_mnt(m,
279                                         mnt->mnt_mountpoint, 0);
280                 /*
281                  * umount the child only if the child has no
282                  * other children
283                  */
284                 if (child && list_empty(&child->mnt_mounts)) {
285                         list_del(&child->mnt_hash);
286                         list_add_tail(&child->mnt_hash, &mnt->mnt_hash);
287                 }
288         }
289 }
290
291 /*
292  * collect all mounts that receive propagation from the mount in @list,
293  * and return these additional mounts in the same list.
294  * @list: the list of mounts to be unmounted.
295  */
296 int propagate_umount(struct list_head *list)
297 {
298         struct vfsmount *mnt;
299
300         list_for_each_entry(mnt, list, mnt_hash)
301                 __propagate_umount(mnt);
302         return 0;
303 }