Whamcloud - gitweb
Branch b1_8
[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,291 @@
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 +       EXT3_I(inode)->i_file_acl = 0;
287 +       inode->i_blocks = 0;
288 +       inode->i_version = 1;
289 +       inode->i_generation = 0;
290 +
291 +       inode->i_op = &iopen_inode_operations;
292 +       inode->i_fop = &iopen_file_operations;
293 +       inode->i_mapping->a_ops = 0;
294 +
295 +       return 1;
296 +}
297 Index: linux-2.6.18-53.1.21/fs/ext3/iopen.h
298 ===================================================================
299 --- /dev/null
300 +++ linux-2.6.18-53.1.21/fs/ext3/iopen.h
301 @@ -0,0 +1,16 @@
302 +/*
303 + * iopen.h
304 + *
305 + * Special support for opening files by inode number.
306 + *
307 + * Copyright (C) 2001 by Theodore Ts'o (tytso@alum.mit.edu).
308 + *
309 + * This file may be redistributed under the terms of the GNU General
310 + * Public License.
311 + */
312 +
313 +extern int ext3_check_for_iopen(struct inode *dir, struct dentry *dentry);
314 +extern int ext3_iopen_get_inode(struct inode *inode);
315 +extern struct dentry *iopen_connect_dentry(struct dentry *dentry,
316 +                                          struct inode *inode, int rehash);
317 +extern void iopen_d_instantiate(struct dentry *dentry, struct inode * inode);
318 Index: linux-2.6.18-53.1.21/fs/ext3/inode.c
319 ===================================================================
320 --- linux-2.6.18-53.1.21.orig/fs/ext3/inode.c
321 +++ linux-2.6.18-53.1.21/fs/ext3/inode.c
322 @@ -37,6 +37,7 @@
323  #include <linux/mpage.h>
324  #include <linux/uio.h>
325  #include "xattr.h"
326 +#include "iopen.h"
327  #include "acl.h"
328  
329  static int ext3_writepage_trans_blocks(struct inode *inode);
330 @@ -2593,6 +2594,8 @@ void ext3_read_inode(struct inode * inod
331         ei->i_default_acl = EXT3_ACL_NOT_CACHED;
332  #endif
333         ei->i_block_alloc_info = NULL;
334 +       if (ext3_iopen_get_inode(inode))
335 +               return;
336  
337         if (__ext3_get_inode_loc(inode, &iloc, 0))
338                 goto bad_inode;
339 Index: linux-2.6.18-53.1.21/fs/ext3/super.c
340 ===================================================================
341 --- linux-2.6.18-53.1.21.orig/fs/ext3/super.c
342 +++ linux-2.6.18-53.1.21/fs/ext3/super.c
343 @@ -677,6 +677,7 @@ enum {
344         Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
345         Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
346         Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
347 +       Opt_iopen, Opt_noiopen, Opt_iopen_nopriv,
348         Opt_grpquota
349  };
350  
351 @@ -726,6 +727,9 @@ static match_table_t tokens = {
352         {Opt_noquota, "noquota"},
353         {Opt_quota, "quota"},
354         {Opt_usrquota, "usrquota"},
355 +       {Opt_iopen, "iopen"},
356 +       {Opt_noiopen, "noiopen"},
357 +       {Opt_iopen_nopriv, "iopen_nopriv"},
358         {Opt_barrier, "barrier=%u"},
359         {Opt_err, NULL},
360         {Opt_resize, "resize"},
361 @@ -1041,6 +1045,18 @@ clear_qf_name:
362                         else
363                                 clear_opt(sbi->s_mount_opt, BARRIER);
364                         break;
365 +               case Opt_iopen:
366 +                       set_opt (sbi->s_mount_opt, IOPEN);
367 +                       clear_opt (sbi->s_mount_opt, IOPEN_NOPRIV);
368 +                       break;
369 +               case Opt_noiopen:
370 +                       clear_opt (sbi->s_mount_opt, IOPEN);
371 +                       clear_opt (sbi->s_mount_opt, IOPEN_NOPRIV);
372 +                       break;
373 +               case Opt_iopen_nopriv:
374 +                       set_opt (sbi->s_mount_opt, IOPEN);
375 +                       set_opt (sbi->s_mount_opt, IOPEN_NOPRIV);
376 +                       break;
377                 case Opt_ignore:
378                         break;
379                 case Opt_resize:
380 Index: linux-2.6.18-53.1.21/fs/ext3/namei.c
381 ===================================================================
382 --- linux-2.6.18-53.1.21.orig/fs/ext3/namei.c
383 +++ linux-2.6.18-53.1.21/fs/ext3/namei.c
384 @@ -39,6 +39,7 @@
385  
386  #include "namei.h"
387  #include "xattr.h"
388 +#include "iopen.h"
389  #include "acl.h"
390  
391  /*
392 @@ -1020,6 +1021,9 @@ static struct dentry *ext3_lookup(struct
393         if (dentry->d_name.len > EXT3_NAME_LEN)
394                 return ERR_PTR(-ENAMETOOLONG);
395  
396 +       if (ext3_check_for_iopen(dir, dentry))
397 +               return NULL;
398 +
399         bh = ext3_find_entry(dentry, &de);
400         inode = NULL;
401         if (bh) {
402 @@ -1035,7 +1039,8 @@ static struct dentry *ext3_lookup(struct
403                 if (!inode)
404                         return ERR_PTR(-EACCES);
405         }
406 -       return d_splice_alias(inode, dentry);
407 +
408 +       return iopen_connect_dentry(dentry, inode, 1);
409  }
410  
411  
412 @@ -1678,7 +1683,7 @@ static int ext3_add_nondir(handle_t *han
413         int err = ext3_add_entry(handle, dentry, inode);
414         if (!err) {
415                 ext3_mark_inode_dirty(handle, inode);
416 -               d_instantiate(dentry, inode);
417 +               iopen_d_instantiate(dentry, inode);
418                 return 0;
419         }
420         ext3_dec_count(handle, inode);
421 @@ -1840,7 +1845,7 @@ retry:
422         dir->i_nlink++;
423         ext3_update_dx_flag(dir);
424         ext3_mark_inode_dirty(handle, dir);
425 -       d_instantiate(dentry, inode);
426 +       iopen_d_instantiate(dentry, inode);
427  out_stop:
428         ext3_journal_stop(handle);
429         if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
430 @@ -2108,10 +2113,6 @@ static int ext3_rmdir (struct inode * di
431                               inode->i_nlink);
432         inode->i_version++;
433         inode->i_nlink = 0;
434 -       /* There's no need to set i_disksize: the fact that i_nlink is
435 -        * zero will ensure that the right thing happens during any
436 -        * recovery. */
437 -       inode->i_size = 0;
438         ext3_orphan_add(handle, inode);
439         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
440         ext3_mark_inode_dirty(handle, inode);
441 @@ -2235,6 +2236,23 @@ out_stop:
442         return err;
443  }
444  
445 +/* Like ext3_add_nondir() except for call to iopen_connect_dentry */
446 +static int ext3_add_link(handle_t *handle, struct dentry *dentry,
447 +                        struct inode *inode)
448 +{
449 +       int err = ext3_add_entry(handle, dentry, inode);
450 +       if (!err) {
451 +               err = ext3_mark_inode_dirty(handle, inode);
452 +               if (err == 0) {
453 +                       dput(iopen_connect_dentry(dentry, inode, 0));
454 +                       return 0;
455 +               }
456 +       }
457 +       ext3_dec_count(handle, inode);
458 +       iput(inode);
459 +       return err;
460 +}
461 +
462  static int ext3_link (struct dentry * old_dentry,
463                 struct inode * dir, struct dentry *dentry)
464  {
465 @@ -2264,7 +2282,8 @@ retry:
466         ext3_inc_count(handle, inode);
467         atomic_inc(&inode->i_count);
468  
469 -       err = ext3_add_nondir(handle, dentry, inode);
470 +       err = ext3_add_link(handle, dentry, inode);
471 +       ext3_orphan_del(handle, inode);
472         ext3_journal_stop(handle);
473         if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
474                 goto retry;
475 Index: linux-2.6.18-53.1.21/fs/ext3/Makefile
476 ===================================================================
477 --- linux-2.6.18-53.1.21.orig/fs/ext3/Makefile
478 +++ linux-2.6.18-53.1.21/fs/ext3/Makefile
479 @@ -4,7 +4,7 @@
480  
481  obj-$(CONFIG_EXT3_FS) += ext3.o
482  
483 -ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o \
484 +ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
485            ioctl.o namei.o super.o symlink.o hash.o resize.o
486  
487  ext3-$(CONFIG_EXT3_FS_XATTR)    += xattr.o xattr_user.o xattr_trusted.o
488 Index: linux-2.6.18-53.1.21/include/linux/ext3_fs.h
489 ===================================================================
490 --- linux-2.6.18-53.1.21.orig/include/linux/ext3_fs.h
491 +++ linux-2.6.18-53.1.21/include/linux/ext3_fs.h
492 @@ -371,6 +371,8 @@ struct ext3_inode {
493  #define EXT3_MOUNT_QUOTA               0x80000 /* Some quota option set */
494  #define EXT3_MOUNT_USRQUOTA            0x100000 /* "old" user quota */
495  #define EXT3_MOUNT_GRPQUOTA            0x200000 /* "old" group quota */
496 +#define EXT3_MOUNT_IOPEN               0x400000        /* Allow access via iopen */
497 +#define EXT3_MOUNT_IOPEN_NOPRIV                0x800000/* Make iopen world-readable */
498  
499  /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
500  #ifndef _LINUX_EXT2_FS_H