Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / iopen-2.6.22-vanilla.patch
1 Index: linux-2.6.16.27-0.9/fs/ext3/iopen.c
2 ===================================================================
3 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
4 +++ linux-2.6.16.27-0.9/fs/ext3/iopen.c 2007-06-29 08:33:12.000000000 +0200
5 @@ -0,0 +1,256 @@
6 +/*
7 + * linux/fs/ext3/iopen.c
8 + *
9 + * Special support for open by inode number
10 + *
11 + * Copyright (C) 2001 by Theodore Ts'o (tytso@alum.mit.edu).
12 + *
13 + * This file may be redistributed under the terms of the GNU General
14 + * Public License.
15 + *
16 + *
17 + * Invariants:
18 + *   - there is only ever a single DCACHE_NFSD_DISCONNECTED dentry alias
19 + *     for an inode at one time.
20 + *   - there are never both connected and DCACHE_NFSD_DISCONNECTED dentry
21 + *     aliases on an inode at the same time.
22 + *
23 + * If we have any connected dentry aliases for an inode, use one of those
24 + * in iopen_lookup().  Otherwise, we instantiate a single NFSD_DISCONNECTED
25 + * dentry for this inode, which thereafter will be found by the dcache
26 + * when looking up this inode number in __iopen__, so we don't return here
27 + * until it is gone.
28 + *
29 + * If we get an inode via a regular name lookup, then we "rename" the
30 + * NFSD_DISCONNECTED dentry to the proper name and parent.  This ensures
31 + * existing users of the disconnected dentry will continue to use the same
32 + * dentry as the connected users, and there will never be both kinds of
33 + * dentry aliases at one time.
34 + */
35 +
36 +#include <linux/sched.h>
37 +#include <linux/fs.h>
38 +#include <linux/ext3_jbd.h>
39 +#include <linux/jbd.h>
40 +#include <linux/ext3_fs.h>
41 +#include <linux/smp_lock.h>
42 +#include <linux/dcache.h>
43 +#include <linux/security.h>
44 +#include "iopen.h"
45 +
46 +#ifndef assert
47 +#define assert(test) J_ASSERT(test)
48 +#endif
49 +
50 +#define IOPEN_NAME_LEN 32
51 +
52 +/*
53 + * This implements looking up an inode by number.
54 + */
55 +static struct dentry *iopen_lookup(struct inode * dir, struct dentry *dentry,
56 +                                  struct nameidata *nd)
57 +{
58 +       struct inode *inode;
59 +       unsigned long ino;
60 +       struct list_head *lp;
61 +       struct dentry *alternate;
62 +       char buf[IOPEN_NAME_LEN];
63 +
64 +       if (dentry->d_name.len >= IOPEN_NAME_LEN)
65 +               return ERR_PTR(-ENAMETOOLONG);
66 +
67 +       memcpy(buf, dentry->d_name.name, dentry->d_name.len);
68 +       buf[dentry->d_name.len] = 0;
69 +
70 +       if (strcmp(buf, ".") == 0)
71 +               ino = dir->i_ino;
72 +       else if (strcmp(buf, "..") == 0)
73 +               ino = EXT3_ROOT_INO;
74 +       else
75 +               ino = simple_strtoul(buf, 0, 0);
76 +
77 +       if ((ino != EXT3_ROOT_INO &&
78 +            //ino != EXT3_ACL_IDX_INO &&
79 +            //ino != EXT3_ACL_DATA_INO &&
80 +            ino < EXT3_FIRST_INO(dir->i_sb)) ||
81 +           ino > le32_to_cpu(EXT3_SB(dir->i_sb)->s_es->s_inodes_count))
82 +               return ERR_PTR(-ENOENT);
83 +
84 +       inode = iget(dir->i_sb, ino);
85 +       if (!inode)
86 +               return ERR_PTR(-EACCES);
87 +       if (is_bad_inode(inode)) {
88 +               iput(inode);
89 +               return ERR_PTR(-ENOENT);
90 +       }
91 +
92 +       assert(list_empty(&dentry->d_alias));           /* d_instantiate */
93 +       assert(d_unhashed(dentry));                     /* d_rehash */
94 +
95 +       /* preferrably return a connected dentry */
96 +       spin_lock(&dcache_lock);
97 +       list_for_each(lp, &inode->i_dentry) {
98 +               alternate = list_entry(lp, struct dentry, d_alias);
99 +               assert(!(alternate->d_flags & DCACHE_DISCONNECTED));
100 +       }
101 +
102 +       if (!list_empty(&inode->i_dentry)) {
103 +               alternate = list_entry(inode->i_dentry.next,
104 +                                      struct dentry, d_alias);
105 +               dget_locked(alternate);
106 +               spin_lock(&alternate->d_lock);
107 +               alternate->d_flags |= DCACHE_REFERENCED;
108 +               spin_unlock(&alternate->d_lock);
109 +               iput(inode);
110 +               spin_unlock(&dcache_lock);
111 +               return alternate;
112 +       }
113 +       dentry->d_flags |= DCACHE_DISCONNECTED;
114 +
115 +       /* d_add(), but don't drop dcache_lock before adding dentry to inode */
116 +       list_add(&dentry->d_alias, &inode->i_dentry);   /* d_instantiate */
117 +       dentry->d_inode = inode;
118 +
119 +       d_rehash_cond(dentry, 0);       /* d_rehash */
120 +       spin_unlock(&dcache_lock);
121 +
122 +       return NULL;
123 +}
124 +
125 +/* This function is spliced into ext3_lookup and does the move of a
126 + * disconnected dentry (if it exists) to a connected dentry.
127 + */
128 +struct dentry *iopen_connect_dentry(struct dentry *dentry, struct inode *inode,
129 +                                   int rehash)
130 +{
131 +       struct dentry *tmp, *goal = NULL;
132 +       struct list_head *lp;
133 +
134 +       /* verify this dentry is really new */
135 +       assert(dentry->d_inode == NULL);
136 +       assert(list_empty(&dentry->d_alias));           /* d_instantiate */
137 +       if (rehash)
138 +               assert(d_unhashed(dentry));             /* d_rehash */
139 +       assert(list_empty(&dentry->d_subdirs));
140 +
141 +       spin_lock(&dcache_lock);
142 +       if (!inode)
143 +               goto do_rehash;
144 +
145 +       if (!test_opt(inode->i_sb, IOPEN))
146 +               goto do_instantiate;
147 +
148 +       /* preferrably return a connected dentry */
149 +       list_for_each(lp, &inode->i_dentry) {
150 +               tmp = list_entry(lp, struct dentry, d_alias);
151 +               if (tmp->d_flags & DCACHE_DISCONNECTED) {
152 +                       assert(tmp->d_alias.next == &inode->i_dentry);
153 +                       assert(tmp->d_alias.prev == &inode->i_dentry);
154 +                       goal = tmp;
155 +                       dget_locked(goal);
156 +                       break;
157 +               }
158 +       }
159 +
160 +       if (!goal)
161 +               goto do_instantiate;
162 +
163 +       /* Move the goal to the de hash queue */
164 +       goal->d_flags &= ~DCACHE_DISCONNECTED;
165 +       security_d_instantiate(goal, inode);
166 +       __d_drop(dentry);
167 +       d_rehash_cond(dentry, 0);
168 +       __d_move(goal, dentry);
169 +       spin_unlock(&dcache_lock);
170 +       iput(inode);
171 +
172 +       return goal;
173 +
174 +       /* d_add(), but don't drop dcache_lock before adding dentry to inode */
175 +do_instantiate:
176 +       list_add(&dentry->d_alias, &inode->i_dentry);   /* d_instantiate */
177 +       dentry->d_inode = inode;
178 +do_rehash:
179 +       if (rehash)
180 +               d_rehash_cond(dentry, 0);       /* d_rehash */
181 +       spin_unlock(&dcache_lock);
182 +
183 +       return NULL;
184 +}
185 +
186 +/*
187 + * These are the special structures for the iopen pseudo directory.
188 + */
189 +
190 +static struct inode_operations iopen_inode_operations = {
191 +       lookup:         iopen_lookup,           /* BKL held */
192 +};
193 +
194 +static struct file_operations iopen_file_operations = {
195 +       read:           generic_read_dir,
196 +};
197 +
198 +static int match_dentry(struct dentry *dentry, const char *name)
199 +{
200 +       int     len;
201 +
202 +       len = strlen(name);
203 +       if (dentry->d_name.len != len)
204 +               return 0;
205 +       if (strncmp(dentry->d_name.name, name, len))
206 +               return 0;
207 +       return 1;
208 +}
209 +
210 +/*
211 + * This function is spliced into ext3_lookup and returns 1 the file
212 + * name is __iopen__ and dentry has been filled in appropriately.
213 + */
214 +int ext3_check_for_iopen(struct inode *dir, struct dentry *dentry)
215 +{
216 +       struct inode *inode;
217 +
218 +       if (dir->i_ino != EXT3_ROOT_INO ||
219 +           !test_opt(dir->i_sb, IOPEN) ||
220 +           !match_dentry(dentry, "__iopen__"))
221 +               return 0;
222 +
223 +       inode = iget(dir->i_sb, EXT3_BAD_INO);
224 +
225 +       if (!inode)
226 +               return 0;
227 +       d_add(dentry, inode);
228 +       return 1;
229 +}
230 +
231 +/*
232 + * This function is spliced into read_inode; it returns 1 if inode
233 + * number is the one for /__iopen__, in which case the inode is filled
234 + * in appropriately.  Otherwise, this fuction returns 0.
235 + */
236 +int ext3_iopen_get_inode(struct inode *inode)
237 +{
238 +       if (inode->i_ino != EXT3_BAD_INO)
239 +               return 0;
240 +
241 +       inode->i_mode = S_IFDIR | S_IRUSR | S_IXUSR;
242 +       if (test_opt(inode->i_sb, IOPEN_NOPRIV))
243 +               inode->i_mode |= 0777;
244 +       inode->i_uid = 0;
245 +       inode->i_gid = 0;
246 +       inode->i_nlink = 1;
247 +       inode->i_size = 4096;
248 +       inode->i_atime = CURRENT_TIME;
249 +       inode->i_ctime = CURRENT_TIME;
250 +       inode->i_mtime = CURRENT_TIME;
251 +       EXT3_I(inode)->i_dtime = 0;
252 +       inode->i_blocks = 0;
253 +       inode->i_version = 1;
254 +       inode->i_generation = 0;
255 +
256 +       inode->i_op = &iopen_inode_operations;
257 +       inode->i_fop = &iopen_file_operations;
258 +       inode->i_mapping->a_ops = 0;
259 +
260 +       return 1;
261 +}
262 Index: linux-2.6.16.27-0.9/fs/ext3/iopen.h
263 ===================================================================
264 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
265 +++ linux-2.6.16.27-0.9/fs/ext3/iopen.h 2007-06-29 08:24:49.000000000 +0200
266 @@ -0,0 +1,15 @@
267 +/*
268 + * iopen.h
269 + *
270 + * Special support for opening files by inode number.
271 + *
272 + * Copyright (C) 2001 by Theodore Ts'o (tytso@alum.mit.edu).
273 + *
274 + * This file may be redistributed under the terms of the GNU General
275 + * Public License.
276 + */
277 +
278 +extern int ext3_check_for_iopen(struct inode *dir, struct dentry *dentry);
279 +extern int ext3_iopen_get_inode(struct inode *inode);
280 +extern struct dentry *iopen_connect_dentry(struct dentry *dentry,
281 +                                          struct inode *inode, int rehash);
282 Index: linux-2.6.16.27-0.9/fs/ext3/inode.c
283 ===================================================================
284 --- linux-2.6.16.27-0.9.orig/fs/ext3/inode.c    2007-06-29 08:24:48.000000000 +0200
285 +++ linux-2.6.16.27-0.9/fs/ext3/inode.c 2007-06-29 08:24:52.000000000 +0200
286 @@ -37,6 +37,7 @@
287  #include <linux/uio.h>
288  #include <linux/bio.h>
289  #include "xattr.h"
290 +#include "iopen.h"
291  #include "acl.h"
292  
293  static int ext3_writepage_trans_blocks(struct inode *inode);
294 @@ -2448,6 +2449,8 @@ void ext3_read_inode(struct inode * inod
295         ei->i_default_acl = EXT3_ACL_NOT_CACHED;
296  #endif
297         ei->i_block_alloc_info = NULL;
298 +       if (ext3_iopen_get_inode(inode))
299 +               return;
300  
301         if (__ext3_get_inode_loc(inode, &iloc, 0))
302                 goto bad_inode;
303 Index: linux-2.6.16.27-0.9/fs/ext3/super.c
304 ===================================================================
305 --- linux-2.6.16.27-0.9.orig/fs/ext3/super.c    2007-06-29 08:24:48.000000000 +0200
306 +++ linux-2.6.16.27-0.9/fs/ext3/super.c 2007-06-29 08:24:52.000000000 +0200
307 @@ -678,6 +678,7 @@ enum {
308         Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
309         Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
310         Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
311 +       Opt_iopen, Opt_noiopen, Opt_iopen_nopriv,
312         Opt_grpquota
313  };
314  
315 @@ -726,6 +727,9 @@ static match_table_t tokens = {
316         {Opt_noquota, "noquota"},
317         {Opt_quota, "quota"},
318         {Opt_usrquota, "usrquota"},
319 +       {Opt_iopen, "iopen"},
320 +       {Opt_noiopen, "noiopen"},
321 +       {Opt_iopen_nopriv, "iopen_nopriv"},
322         {Opt_barrier, "barrier=%u"},
323         {Opt_err, NULL},
324         {Opt_resize, "resize"},
325 @@ -1040,6 +1044,18 @@ clear_qf_name:
326                         else
327                                 clear_opt(sbi->s_mount_opt, BARRIER);
328                         break;
329 +               case Opt_iopen:
330 +                       set_opt (sbi->s_mount_opt, IOPEN);
331 +                       clear_opt (sbi->s_mount_opt, IOPEN_NOPRIV);
332 +                       break;
333 +               case Opt_noiopen:
334 +                       clear_opt (sbi->s_mount_opt, IOPEN);
335 +                       clear_opt (sbi->s_mount_opt, IOPEN_NOPRIV);
336 +                       break;
337 +               case Opt_iopen_nopriv:
338 +                       set_opt (sbi->s_mount_opt, IOPEN);
339 +                       set_opt (sbi->s_mount_opt, IOPEN_NOPRIV);
340 +                       break;
341                 case Opt_ignore:
342                         break;
343                 case Opt_resize:
344 Index: linux-2.6.16.27-0.9/fs/ext3/namei.c
345 ===================================================================
346 --- linux-2.6.16.27-0.9.orig/fs/ext3/namei.c    2007-06-29 08:24:47.000000000 +0200
347 +++ linux-2.6.16.27-0.9/fs/ext3/namei.c 2007-06-29 08:24:49.000000000 +0200
348 @@ -39,6 +39,7 @@
349  
350  #include "namei.h"
351  #include "xattr.h"
352 +#include "iopen.h"
353  #include "acl.h"
354  
355  /*
356 @@ -1004,6 +1005,9 @@ static struct dentry *ext3_lookup(struct
357         if (dentry->d_name.len > EXT3_NAME_LEN)
358                 return ERR_PTR(-ENAMETOOLONG);
359  
360 +       if (ext3_check_for_iopen(dir, dentry))
361 +               return NULL;
362 +
363         bh = ext3_find_entry(dentry, &de);
364         inode = NULL;
365         if (bh) {
366 @@ -1014,7 +1018,7 @@ static struct dentry *ext3_lookup(struct
367                 if (!inode)
368                         return ERR_PTR(-EACCES);
369         }
370 -       return d_splice_alias(inode, dentry);
371 +       return iopen_connect_dentry(dentry, inode, 1);
372  }
373  
374  
375 @@ -2058,10 +2062,6 @@ static int ext3_rmdir (struct inode * di
376                               inode->i_nlink);
377         inode->i_version++;
378         clear_nlink(inode);
379 -       /* There's no need to set i_disksize: the fact that i_nlink is
380 -        * zero will ensure that the right thing happens during any
381 -        * recovery. */
382 -       inode->i_size = 0;
383         ext3_orphan_add(handle, inode);
384         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
385         ext3_mark_inode_dirty(handle, inode);
386 @@ -2185,6 +2185,23 @@ out_stop:
387         return err;
388  }
389  
390 +/* Like ext3_add_nondir() except for call to iopen_connect_dentry */
391 +static int ext3_add_link(handle_t *handle, struct dentry *dentry,
392 +                        struct inode *inode)
393 +{
394 +       int err = ext3_add_entry(handle, dentry, inode);
395 +       if (!err) {
396 +               err = ext3_mark_inode_dirty(handle, inode);
397 +               if (err == 0) {
398 +                       dput(iopen_connect_dentry(dentry, inode, 0));
399 +                       return 0;
400 +               }
401 +       }
402 +       ext3_dec_count(handle, inode);
403 +       iput(inode);
404 +       return err;
405 +}
406 +
407  static int ext3_link (struct dentry * old_dentry,
408                 struct inode * dir, struct dentry *dentry)
409  {
410 @@ -2208,7 +2225,8 @@ retry:
411         ext3_inc_count(handle, inode);
412         atomic_inc(&inode->i_count);
413  
414 -       err = ext3_add_nondir(handle, dentry, inode);
415 +       err = ext3_add_link(handle, dentry, inode);
416 +       ext3_orphan_del(handle, inode);
417         ext3_journal_stop(handle);
418         if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
419                 goto retry;
420 Index: linux-2.6.16.27-0.9/fs/ext3/Makefile
421 ===================================================================
422 --- linux-2.6.16.27-0.9.orig/fs/ext3/Makefile   2007-03-13 00:56:52.000000000 +0100
423 +++ linux-2.6.16.27-0.9/fs/ext3/Makefile        2007-06-29 08:24:49.000000000 +0200
424 @@ -4,7 +4,7 @@
425  
426  obj-$(CONFIG_EXT3_FS) += ext3.o
427  
428 -ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o \
429 +ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
430            ioctl.o namei.o super.o symlink.o hash.o resize.o ext3_jbd.o
431  
432  ext3-$(CONFIG_EXT3_FS_XATTR)    += xattr.o xattr_user.o xattr_trusted.o
433 Index: linux-2.6.16.27-0.9/include/linux/ext3_fs.h
434 ===================================================================
435 --- linux-2.6.16.27-0.9.orig/include/linux/ext3_fs.h    2007-06-29 08:24:47.000000000 +0200
436 +++ linux-2.6.16.27-0.9/include/linux/ext3_fs.h 2007-06-29 08:24:49.000000000 +0200
437 @@ -375,6 +375,8 @@ struct ext3_inode {
438  #define EXT3_MOUNT_QUOTA               0x80000 /* Some quota option set */
439  #define EXT3_MOUNT_USRQUOTA            0x100000 /* "old" user quota */
440  #define EXT3_MOUNT_GRPQUOTA            0x200000 /* "old" group quota */
441 +#define EXT3_MOUNT_IOPEN               0x400000        /* Allow access via iopen */
442 +#define EXT3_MOUNT_IOPEN_NOPRIV                0x800000/* Make iopen world-readable */
443  
444  /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
445  #ifndef _LINUX_EXT2_FS_H