Whamcloud - gitweb
Branch b1_6
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / iopen-2.6.18-rhel5.patch
1 Index: linux-2.6.18-53.1.21/fs/ext3/iopen.c
2 ===================================================================
3 --- /dev/null
4 +++ linux-2.6.18-53.1.21/fs/ext3/iopen.c
5 @@ -0,0 +1,290 @@
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_FIRST_INO(dir->i_sb)) ||
79 +           ino > le32_to_cpu(EXT3_SB(dir->i_sb)->s_es->s_inodes_count))
80 +               return ERR_PTR(-ENOENT);
81 +
82 +       inode = iget(dir->i_sb, ino);
83 +       if (!inode)
84 +               return ERR_PTR(-EACCES);
85 +       if (is_bad_inode(inode)) {
86 +               iput(inode);
87 +               return ERR_PTR(-ENOENT);
88 +       }
89 +
90 +       assert(list_empty(&dentry->d_alias));           /* d_instantiate */
91 +       assert(d_unhashed(dentry));                     /* d_rehash */
92 +
93 +       /* preferrably return a connected dentry */
94 +       spin_lock(&dcache_lock);
95 +       list_for_each(lp, &inode->i_dentry) {
96 +               alternate = list_entry(lp, struct dentry, d_alias);
97 +               assert(!(alternate->d_flags & DCACHE_DISCONNECTED));
98 +       }
99 +
100 +       if (!list_empty(&inode->i_dentry)) {
101 +               alternate = list_entry(inode->i_dentry.next,
102 +                                      struct dentry, d_alias);
103 +               dget_locked(alternate);
104 +               spin_lock(&alternate->d_lock);
105 +               alternate->d_flags |= DCACHE_REFERENCED;
106 +               spin_unlock(&alternate->d_lock);
107 +               iput(inode);
108 +               spin_unlock(&dcache_lock);
109 +               return alternate;
110 +       }
111 +       dentry->d_flags |= DCACHE_DISCONNECTED;
112 +
113 +       /* d_add(), but don't drop dcache_lock before adding dentry to inode */
114 +       list_add(&dentry->d_alias, &inode->i_dentry);   /* d_instantiate */
115 +       dentry->d_inode = inode;
116 +
117 +       d_rehash_cond(dentry, 0);
118 +       spin_unlock(&dcache_lock);
119 +
120 +       return NULL;
121 +}
122 +
123 +/* This function is spliced into ext3_lookup and does the move of a
124 + * disconnected dentry (if it exists) to a connected dentry.
125 + */
126 +struct dentry *iopen_connect_dentry(struct dentry *dentry, struct inode *inode,
127 +                                   int rehash)
128 +{
129 +       struct dentry *tmp, *goal = NULL;
130 +       struct list_head *lp;
131 +
132 +       /* verify this dentry is really new */
133 +       assert(dentry->d_inode == NULL);
134 +       assert(list_empty(&dentry->d_alias));           /* d_instantiate */
135 +       if (rehash)
136 +               assert(d_unhashed(dentry));             /* d_rehash */
137 +       assert(list_empty(&dentry->d_subdirs));
138 +
139 +       spin_lock(&dcache_lock);
140 +       if (!inode)
141 +               goto do_rehash;
142 +
143 +       if (!test_opt(inode->i_sb, IOPEN))
144 +               goto do_instantiate;
145 +
146 +       /* preferrably return a connected dentry */
147 +       list_for_each(lp, &inode->i_dentry) {
148 +               tmp = list_entry(lp, struct dentry, d_alias);
149 +               if (tmp->d_flags & DCACHE_DISCONNECTED) {
150 +                       assert(tmp->d_alias.next == &inode->i_dentry);
151 +                       assert(tmp->d_alias.prev == &inode->i_dentry);
152 +                       goal = tmp;
153 +                       dget_locked(goal);
154 +                       break;
155 +               }
156 +       }
157 +
158 +       if (!goal)
159 +               goto do_instantiate;
160 +
161 +       /* Move the goal to the de hash queue */
162 +       goal->d_flags &= ~DCACHE_DISCONNECTED;
163 +       security_d_instantiate(goal, inode);
164 +       __d_drop(dentry);
165 +       d_rehash_cond(dentry, 0);
166 +       d_move_locked(goal, dentry);
167 +       spin_unlock(&dcache_lock);
168 +       iput(inode);
169 +
170 +       return goal;
171 +
172 +       /* d_add(), but don't drop dcache_lock before adding dentry to inode */
173 +do_instantiate:
174 +       list_add(&dentry->d_alias, &inode->i_dentry);   /* d_instantiate */
175 +       dentry->d_inode = inode;
176 +do_rehash:
177 +       if (rehash)
178 +               d_rehash_cond(dentry, 0);
179 +       spin_unlock(&dcache_lock);
180 +
181 +       return NULL;
182 +}
183 +
184 +/*
185 + * Similar as d_instantiate() except that it drops the disconnected
186 + * dentry if any.
187 + */
188 +void iopen_d_instantiate(struct dentry *dentry, struct inode * inode)
189 +{
190 +       struct dentry *dis_dentry;
191 +
192 +       /* verify this dentry is really new */
193 +       assert(dentry->d_inode == NULL);
194 +       assert(list_empty(&dentry->d_alias));
195 +
196 +       spin_lock(&dcache_lock);
197 +       if (!inode || !test_opt(inode->i_sb, IOPEN) ||
198 +           list_empty(&inode->i_dentry))
199 +               goto do_instantiate;
200 +
201 +       /* a disconnected dentry has been added in our back,
202 +        * we have to drop this dentry, see bug 16362/15713*/
203 +       dis_dentry = list_entry(inode->i_dentry.next, struct dentry, d_alias);
204 +       spin_lock(&dis_dentry->d_lock);
205 +       assert(dis_dentry->d_alias.next == &inode->i_dentry);
206 +       assert(dis_dentry->d_alias.prev == &inode->i_dentry);
207 +       assert(dis_dentry->d_flags & DCACHE_DISCONNECTED);
208 +       __d_drop(dis_dentry);
209 +       list_del_init(&dis_dentry->d_alias);
210 +       spin_unlock(&dis_dentry->d_lock);
211 +
212 +do_instantiate:
213 +       if (inode)
214 +               list_add(&dentry->d_alias, &inode->i_dentry);
215 +       dentry->d_inode = inode;
216 +       spin_unlock(&dcache_lock);
217 +       security_d_instantiate(dentry, inode);
218 +}
219 +
220 +/*
221 + * These are the special structures for the iopen pseudo directory.
222 + */
223 +
224 +static struct inode_operations iopen_inode_operations = {
225 +       lookup:         iopen_lookup,           /* BKL held */
226 +};
227 +
228 +static struct file_operations iopen_file_operations = {
229 +       read:           generic_read_dir,
230 +};
231 +
232 +static int match_dentry(struct dentry *dentry, const char *name)
233 +{
234 +       int     len;
235 +
236 +       len = strlen(name);
237 +       if (dentry->d_name.len != len)
238 +               return 0;
239 +       if (strncmp(dentry->d_name.name, name, len))
240 +               return 0;
241 +       return 1;
242 +}
243 +
244 +/*
245 + * This function is spliced into ext3_lookup and returns 1 the file
246 + * name is __iopen__ and dentry has been filled in appropriately.
247 + */
248 +int ext3_check_for_iopen(struct inode *dir, struct dentry *dentry)
249 +{
250 +       struct inode *inode;
251 +
252 +       if (dir->i_ino != EXT3_ROOT_INO ||
253 +           !test_opt(dir->i_sb, IOPEN) ||
254 +           !match_dentry(dentry, "__iopen__"))
255 +               return 0;
256 +
257 +       inode = iget(dir->i_sb, EXT3_BAD_INO);
258 +
259 +       if (!inode)
260 +               return 0;
261 +       d_add(dentry, inode);
262 +       return 1;
263 +}
264 +
265 +/*
266 + * This function is spliced into read_inode; it returns 1 if inode
267 + * number is the one for /__iopen__, in which case the inode is filled
268 + * in appropriately.  Otherwise, this fuction returns 0.
269 + */
270 +int ext3_iopen_get_inode(struct inode *inode)
271 +{
272 +       if (inode->i_ino != EXT3_BAD_INO)
273 +               return 0;
274 +
275 +       inode->i_mode = S_IFDIR | S_IRUSR | S_IXUSR;
276 +       if (test_opt(inode->i_sb, IOPEN_NOPRIV))
277 +               inode->i_mode |= 0777;
278 +       inode->i_uid = 0;
279 +       inode->i_gid = 0;
280 +       inode->i_nlink = 1;
281 +       inode->i_size = 4096;
282 +       inode->i_atime = CURRENT_TIME;
283 +       inode->i_ctime = CURRENT_TIME;
284 +       inode->i_mtime = CURRENT_TIME;
285 +       EXT3_I(inode)->i_dtime = 0;
286 +       inode->i_blocks = 0;
287 +       inode->i_version = 1;
288 +       inode->i_generation = 0;
289 +
290 +       inode->i_op = &iopen_inode_operations;
291 +       inode->i_fop = &iopen_file_operations;
292 +       inode->i_mapping->a_ops = 0;
293 +
294 +       return 1;
295 +}
296 Index: linux-2.6.18-53.1.21/fs/ext3/iopen.h
297 ===================================================================
298 --- /dev/null
299 +++ linux-2.6.18-53.1.21/fs/ext3/iopen.h
300 @@ -0,0 +1,16 @@
301 +/*
302 + * iopen.h
303 + *
304 + * Special support for opening files by inode number.
305 + *
306 + * Copyright (C) 2001 by Theodore Ts'o (tytso@alum.mit.edu).
307 + *
308 + * This file may be redistributed under the terms of the GNU General
309 + * Public License.
310 + */
311 +
312 +extern int ext3_check_for_iopen(struct inode *dir, struct dentry *dentry);
313 +extern int ext3_iopen_get_inode(struct inode *inode);
314 +extern struct dentry *iopen_connect_dentry(struct dentry *dentry,
315 +                                          struct inode *inode, int rehash);
316 +extern void iopen_d_instantiate(struct dentry *dentry, struct inode * inode);
317 Index: linux-2.6.18-53.1.21/fs/ext3/inode.c
318 ===================================================================
319 --- linux-2.6.18-53.1.21.orig/fs/ext3/inode.c
320 +++ linux-2.6.18-53.1.21/fs/ext3/inode.c
321 @@ -37,6 +37,7 @@
322  #include <linux/mpage.h>
323  #include <linux/uio.h>
324  #include "xattr.h"
325 +#include "iopen.h"
326  #include "acl.h"
327  
328  static int ext3_writepage_trans_blocks(struct inode *inode);
329 @@ -2593,6 +2594,8 @@ void ext3_read_inode(struct inode * inod
330         ei->i_default_acl = EXT3_ACL_NOT_CACHED;
331  #endif
332         ei->i_block_alloc_info = NULL;
333 +       if (ext3_iopen_get_inode(inode))
334 +               return;
335  
336         if (__ext3_get_inode_loc(inode, &iloc, 0))
337                 goto bad_inode;
338 Index: linux-2.6.18-53.1.21/fs/ext3/super.c
339 ===================================================================
340 --- linux-2.6.18-53.1.21.orig/fs/ext3/super.c
341 +++ linux-2.6.18-53.1.21/fs/ext3/super.c
342 @@ -677,6 +677,7 @@ enum {
343         Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
344         Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
345         Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
346 +       Opt_iopen, Opt_noiopen, Opt_iopen_nopriv,
347         Opt_grpquota
348  };
349  
350 @@ -726,6 +727,9 @@ static match_table_t tokens = {
351         {Opt_noquota, "noquota"},
352         {Opt_quota, "quota"},
353         {Opt_usrquota, "usrquota"},
354 +       {Opt_iopen, "iopen"},
355 +       {Opt_noiopen, "noiopen"},
356 +       {Opt_iopen_nopriv, "iopen_nopriv"},
357         {Opt_barrier, "barrier=%u"},
358         {Opt_err, NULL},
359         {Opt_resize, "resize"},
360 @@ -1041,6 +1045,18 @@ clear_qf_name:
361                         else
362                                 clear_opt(sbi->s_mount_opt, BARRIER);
363                         break;
364 +               case Opt_iopen:
365 +                       set_opt (sbi->s_mount_opt, IOPEN);
366 +                       clear_opt (sbi->s_mount_opt, IOPEN_NOPRIV);
367 +                       break;
368 +               case Opt_noiopen:
369 +                       clear_opt (sbi->s_mount_opt, IOPEN);
370 +                       clear_opt (sbi->s_mount_opt, IOPEN_NOPRIV);
371 +                       break;
372 +               case Opt_iopen_nopriv:
373 +                       set_opt (sbi->s_mount_opt, IOPEN);
374 +                       set_opt (sbi->s_mount_opt, IOPEN_NOPRIV);
375 +                       break;
376                 case Opt_ignore:
377                         break;
378                 case Opt_resize:
379 Index: linux-2.6.18-53.1.21/fs/ext3/namei.c
380 ===================================================================
381 --- linux-2.6.18-53.1.21.orig/fs/ext3/namei.c
382 +++ linux-2.6.18-53.1.21/fs/ext3/namei.c
383 @@ -39,6 +39,7 @@
384  
385  #include "namei.h"
386  #include "xattr.h"
387 +#include "iopen.h"
388  #include "acl.h"
389  
390  /*
391 @@ -1020,6 +1021,9 @@ static struct dentry *ext3_lookup(struct
392         if (dentry->d_name.len > EXT3_NAME_LEN)
393                 return ERR_PTR(-ENAMETOOLONG);
394  
395 +       if (ext3_check_for_iopen(dir, dentry))
396 +               return NULL;
397 +
398         bh = ext3_find_entry(dentry, &de);
399         inode = NULL;
400         if (bh) {
401 @@ -1035,7 +1039,8 @@ static struct dentry *ext3_lookup(struct
402                 if (!inode)
403                         return ERR_PTR(-EACCES);
404         }
405 -       return d_splice_alias(inode, dentry);
406 +
407 +       return iopen_connect_dentry(dentry, inode, 1);
408  }
409  
410  
411 @@ -1678,7 +1683,7 @@ static int ext3_add_nondir(handle_t *han
412         int err = ext3_add_entry(handle, dentry, inode);
413         if (!err) {
414                 ext3_mark_inode_dirty(handle, inode);
415 -               d_instantiate(dentry, inode);
416 +               iopen_d_instantiate(dentry, inode);
417                 return 0;
418         }
419         ext3_dec_count(handle, inode);
420 @@ -1840,7 +1845,7 @@ retry:
421         dir->i_nlink++;
422         ext3_update_dx_flag(dir);
423         ext3_mark_inode_dirty(handle, dir);
424 -       d_instantiate(dentry, inode);
425 +       iopen_d_instantiate(dentry, inode);
426  out_stop:
427         ext3_journal_stop(handle);
428         if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
429 @@ -2108,10 +2113,6 @@ static int ext3_rmdir (struct inode * di
430                               inode->i_nlink);
431         inode->i_version++;
432         inode->i_nlink = 0;
433 -       /* There's no need to set i_disksize: the fact that i_nlink is
434 -        * zero will ensure that the right thing happens during any
435 -        * recovery. */
436 -       inode->i_size = 0;
437         ext3_orphan_add(handle, inode);
438         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
439         ext3_mark_inode_dirty(handle, inode);
440 @@ -2235,6 +2236,23 @@ out_stop:
441         return err;
442  }
443  
444 +/* Like ext3_add_nondir() except for call to iopen_connect_dentry */
445 +static int ext3_add_link(handle_t *handle, struct dentry *dentry,
446 +                        struct inode *inode)
447 +{
448 +       int err = ext3_add_entry(handle, dentry, inode);
449 +       if (!err) {
450 +               err = ext3_mark_inode_dirty(handle, inode);
451 +               if (err == 0) {
452 +                       dput(iopen_connect_dentry(dentry, inode, 0));
453 +                       return 0;
454 +               }
455 +       }
456 +       ext3_dec_count(handle, inode);
457 +       iput(inode);
458 +       return err;
459 +}
460 +
461  static int ext3_link (struct dentry * old_dentry,
462                 struct inode * dir, struct dentry *dentry)
463  {
464 @@ -2264,7 +2282,8 @@ retry:
465         ext3_inc_count(handle, inode);
466         atomic_inc(&inode->i_count);
467  
468 -       err = ext3_add_nondir(handle, dentry, inode);
469 +       err = ext3_add_link(handle, dentry, inode);
470 +       ext3_orphan_del(handle, inode);
471         ext3_journal_stop(handle);
472         if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
473                 goto retry;
474 Index: linux-2.6.18-53.1.21/fs/ext3/Makefile
475 ===================================================================
476 --- linux-2.6.18-53.1.21.orig/fs/ext3/Makefile
477 +++ linux-2.6.18-53.1.21/fs/ext3/Makefile
478 @@ -4,7 +4,7 @@
479  
480  obj-$(CONFIG_EXT3_FS) += ext3.o
481  
482 -ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o \
483 +ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
484            ioctl.o namei.o super.o symlink.o hash.o resize.o
485  
486  ext3-$(CONFIG_EXT3_FS_XATTR)    += xattr.o xattr_user.o xattr_trusted.o
487 Index: linux-2.6.18-53.1.21/include/linux/ext3_fs.h
488 ===================================================================
489 --- linux-2.6.18-53.1.21.orig/include/linux/ext3_fs.h
490 +++ linux-2.6.18-53.1.21/include/linux/ext3_fs.h
491 @@ -371,6 +371,8 @@ struct ext3_inode {
492  #define EXT3_MOUNT_QUOTA               0x80000 /* Some quota option set */
493  #define EXT3_MOUNT_USRQUOTA            0x100000 /* "old" user quota */
494  #define EXT3_MOUNT_GRPQUOTA            0x200000 /* "old" group quota */
495 +#define EXT3_MOUNT_IOPEN               0x400000        /* Allow access via iopen */
496 +#define EXT3_MOUNT_IOPEN_NOPRIV                0x800000/* Make iopen world-readable */
497  
498  /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
499  #ifndef _LINUX_EXT2_FS_H