Whamcloud - gitweb
b=20298 (Merge head ldiskfs and b1_8 ldiskfs)
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / iopen-2.6-fc5.patch
1 Index: linux-2.6.16.54-0.2.5/fs/ext3/iopen.c
2 ===================================================================
3 --- /dev/null
4 +++ linux-2.6.16.54-0.2.5/fs/ext3/iopen.c
5 @@ -0,0 +1,296 @@
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_locked(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 + * Similar as d_instantiate() except that it drops the disconnected
188 + * dentry if any.
189 + */
190 +void iopen_d_instantiate(struct dentry *dentry, struct inode * inode)
191 +{
192 +       struct dentry *dis_dentry;
193 +
194 +       /* verify this dentry is really new */
195 +       assert(dentry->d_inode == NULL);
196 +       assert(list_empty(&dentry->d_alias));
197 +
198 +       spin_lock(&dcache_lock);
199 +       if (!inode || !test_opt(inode->i_sb, IOPEN) ||
200 +           list_empty(&inode->i_dentry))
201 +               goto do_instantiate;
202 +
203 +       /* a disconnected dentry has been added in our back,
204 +        * we have to drop this dentry, see bug 16362/15713*/
205 +       dis_dentry = list_entry(inode->i_dentry.next, struct dentry, d_alias);
206 +       spin_lock(&dis_dentry->d_lock);
207 +       assert(dis_dentry->d_alias.next == &inode->i_dentry);
208 +       assert(dis_dentry->d_alias.prev == &inode->i_dentry);
209 +       assert(dis_dentry->d_flags & DCACHE_DISCONNECTED);
210 +       __d_drop(dis_dentry);
211 +       list_del_init(&dis_dentry->d_alias);
212 +       spin_unlock(&dis_dentry->d_lock);
213 +
214 +do_instantiate:
215 +       if (inode)
216 +               list_add(&dentry->d_alias, &inode->i_dentry);
217 +       dentry->d_inode = inode;
218 +       spin_unlock(&dcache_lock);
219 +       security_d_instantiate(dentry, inode);
220 +}
221 +
222 +/*
223 + * These are the special structures for the iopen pseudo directory.
224 + */
225 +
226 +static struct inode_operations iopen_inode_operations = {
227 +       lookup:         iopen_lookup,           /* BKL held */
228 +};
229 +
230 +static struct file_operations iopen_file_operations = {
231 +       read:           generic_read_dir,
232 +};
233 +
234 +static int match_dentry(struct dentry *dentry, const char *name)
235 +{
236 +       int     len;
237 +
238 +       len = strlen(name);
239 +       if (dentry->d_name.len != len)
240 +               return 0;
241 +       if (strncmp(dentry->d_name.name, name, len))
242 +               return 0;
243 +       return 1;
244 +}
245 +
246 +/*
247 + * This function is spliced into ext3_lookup and returns 1 the file
248 + * name is __iopen__ and dentry has been filled in appropriately.
249 + */
250 +int ext3_check_for_iopen(struct inode *dir, struct dentry *dentry)
251 +{
252 +       struct inode *inode;
253 +
254 +       if (dir->i_ino != EXT3_ROOT_INO ||
255 +           !test_opt(dir->i_sb, IOPEN) ||
256 +           !match_dentry(dentry, "__iopen__"))
257 +               return 0;
258 +
259 +       inode = iget(dir->i_sb, EXT3_BAD_INO);
260 +
261 +       if (!inode)
262 +               return 0;
263 +       d_add(dentry, inode);
264 +       return 1;
265 +}
266 +
267 +/*
268 + * This function is spliced into read_inode; it returns 1 if inode
269 + * number is the one for /__iopen__, in which case the inode is filled
270 + * in appropriately.  Otherwise, this fuction returns 0.
271 + */
272 +int ext3_iopen_get_inode(struct inode *inode)
273 +{
274 +       if (inode->i_ino != EXT3_BAD_INO)
275 +               return 0;
276 +
277 +       inode->i_mode = S_IFDIR | S_IRUSR | S_IXUSR;
278 +       if (test_opt(inode->i_sb, IOPEN_NOPRIV))
279 +               inode->i_mode |= 0777;
280 +       inode->i_uid = 0;
281 +       inode->i_gid = 0;
282 +       inode->i_nlink = 1;
283 +       inode->i_size = 4096;
284 +       inode->i_atime = CURRENT_TIME;
285 +       inode->i_ctime = CURRENT_TIME;
286 +       inode->i_mtime = CURRENT_TIME;
287 +       EXT3_I(inode)->i_dtime = 0;
288 +       EXT3_I(inode)->i_file_acl = 0;
289 +       inode->i_blksize = PAGE_SIZE;   /* This is the optimal IO size
290 +                                        * (for stat), not the fs block
291 +                                        * size */
292 +       inode->i_blocks = 0;
293 +       inode->i_version = 1;
294 +       inode->i_generation = 0;
295 +
296 +       inode->i_op = &iopen_inode_operations;
297 +       inode->i_fop = &iopen_file_operations;
298 +       inode->i_mapping->a_ops = 0;
299 +
300 +       return 1;
301 +}
302 Index: linux-2.6.16.54-0.2.5/fs/ext3/iopen.h
303 ===================================================================
304 --- /dev/null
305 +++ linux-2.6.16.54-0.2.5/fs/ext3/iopen.h
306 @@ -0,0 +1,20 @@
307 +/*
308 + * iopen.h
309 + *
310 + * Special support for opening files by inode number.
311 + *
312 + * Copyright (C) 2001 by Theodore Ts'o (tytso@alum.mit.edu).
313 + *
314 + * This file may be redistributed under the terms of the GNU General
315 + * Public License.
316 + */
317 +
318 +extern int ext3_check_for_iopen(struct inode *dir, struct dentry *dentry);
319 +extern int ext3_iopen_get_inode(struct inode *inode);
320 +extern struct dentry *iopen_connect_dentry(struct dentry *dentry,
321 +                                          struct inode *inode, int rehash);
322 +extern void iopen_d_instantiate(struct dentry *dentry, struct inode * inode);
323 +
324 +#if !defined(HAVE_D_MOVE_LOCKED) && defined(HAVE___D_MOVE)
325 +#define d_move_locked(dentry, target) __d_move(dentry, target)
326 +#endif
327 Index: linux-2.6.16.54-0.2.5/fs/ext3/inode.c
328 ===================================================================
329 --- linux-2.6.16.54-0.2.5.orig/fs/ext3/inode.c
330 +++ linux-2.6.16.54-0.2.5/fs/ext3/inode.c
331 @@ -37,6 +37,7 @@
332  #include <linux/mpage.h>
333  #include <linux/uio.h>
334  #include "xattr.h"
335 +#include "iopen.h"
336  #include "acl.h"
337  
338  static int ext3_writepage_trans_blocks(struct inode *inode);
339 @@ -2448,6 +2449,8 @@ void ext3_read_inode(struct inode * inod
340         ei->i_default_acl = EXT3_ACL_NOT_CACHED;
341  #endif
342         ei->i_block_alloc_info = NULL;
343 +       if (ext3_iopen_get_inode(inode))
344 +               return;
345  
346         if (__ext3_get_inode_loc(inode, &iloc, 0))
347                 goto bad_inode;
348 Index: linux-2.6.16.54-0.2.5/fs/ext3/super.c
349 ===================================================================
350 --- linux-2.6.16.54-0.2.5.orig/fs/ext3/super.c
351 +++ linux-2.6.16.54-0.2.5/fs/ext3/super.c
352 @@ -675,6 +675,7 @@ enum {
353         Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
354         Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
355         Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
356 +       Opt_iopen, Opt_noiopen, Opt_iopen_nopriv,
357         Opt_grpquota
358  };
359  
360 @@ -723,6 +724,9 @@ static match_table_t tokens = {
361         {Opt_noquota, "noquota"},
362         {Opt_quota, "quota"},
363         {Opt_usrquota, "usrquota"},
364 +       {Opt_iopen, "iopen"},
365 +       {Opt_noiopen, "noiopen"},
366 +       {Opt_iopen_nopriv, "iopen_nopriv"},
367         {Opt_barrier, "barrier=%u"},
368         {Opt_err, NULL},
369         {Opt_resize, "resize"},
370 @@ -1037,6 +1041,18 @@ clear_qf_name:
371                         else
372                                 clear_opt(sbi->s_mount_opt, BARRIER);
373                         break;
374 +               case Opt_iopen:
375 +                       set_opt (sbi->s_mount_opt, IOPEN);
376 +                       clear_opt (sbi->s_mount_opt, IOPEN_NOPRIV);
377 +                       break;
378 +               case Opt_noiopen:
379 +                       clear_opt (sbi->s_mount_opt, IOPEN);
380 +                       clear_opt (sbi->s_mount_opt, IOPEN_NOPRIV);
381 +                       break;
382 +               case Opt_iopen_nopriv:
383 +                       set_opt (sbi->s_mount_opt, IOPEN);
384 +                       set_opt (sbi->s_mount_opt, IOPEN_NOPRIV);
385 +                       break;
386                 case Opt_ignore:
387                         break;
388                 case Opt_resize:
389 Index: linux-2.6.16.54-0.2.5/fs/ext3/namei.c
390 ===================================================================
391 --- linux-2.6.16.54-0.2.5.orig/fs/ext3/namei.c
392 +++ linux-2.6.16.54-0.2.5/fs/ext3/namei.c
393 @@ -39,6 +39,7 @@
394  
395  #include "namei.h"
396  #include "xattr.h"
397 +#include "iopen.h"
398  #include "acl.h"
399  
400  /*
401 @@ -1013,6 +1014,9 @@ static struct dentry *ext3_lookup(struct
402         if (dentry->d_name.len > EXT3_NAME_LEN)
403                 return ERR_PTR(-ENAMETOOLONG);
404  
405 +       if (ext3_check_for_iopen(dir, dentry))
406 +               return NULL;
407 +
408         bh = ext3_find_entry(dentry, &de);
409         inode = NULL;
410         if (bh) {
411 @@ -1028,7 +1032,8 @@ static struct dentry *ext3_lookup(struct
412                 if (!inode)
413                         return ERR_PTR(-EACCES);
414         }
415 -       return d_splice_alias(inode, dentry);
416 +
417 +       return iopen_connect_dentry(dentry, inode, 1);
418  }
419  
420  
421 @@ -1649,7 +1654,7 @@ static int ext3_add_nondir(handle_t *han
422         int err = ext3_add_entry(handle, dentry, inode);
423         if (!err) {
424                 ext3_mark_inode_dirty(handle, inode);
425 -               d_instantiate(dentry, inode);
426 +               iopen_d_instantiate(dentry, inode);
427                 return 0;
428         }
429         ext3_dec_count(handle, inode);
430 @@ -1811,7 +1816,7 @@ retry:
431         dir->i_nlink++;
432         ext3_update_dx_flag(dir);
433         ext3_mark_inode_dirty(handle, dir);
434 -       d_instantiate(dentry, inode);
435 +       iopen_d_instantiate(dentry, inode);
436  out_stop:
437         ext3_journal_stop(handle);
438         if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
439 @@ -2079,10 +2084,6 @@ static int ext3_rmdir (struct inode * di
440                               inode->i_nlink);
441         inode->i_version++;
442         inode->i_nlink = 0;
443 -       /* There's no need to set i_disksize: the fact that i_nlink is
444 -        * zero will ensure that the right thing happens during any
445 -        * recovery. */
446 -       inode->i_size = 0;
447         ext3_orphan_add(handle, inode);
448         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
449         ext3_mark_inode_dirty(handle, inode);
450 @@ -2206,6 +2207,23 @@ out_stop:
451         return err;
452  }
453  
454 +/* Like ext3_add_nondir() except for call to iopen_connect_dentry */
455 +static int ext3_add_link(handle_t *handle, struct dentry *dentry,
456 +                        struct inode *inode)
457 +{
458 +       int err = ext3_add_entry(handle, dentry, inode);
459 +       if (!err) {
460 +               err = ext3_mark_inode_dirty(handle, inode);
461 +               if (err == 0) {
462 +                       dput(iopen_connect_dentry(dentry, inode, 0));
463 +                       return 0;
464 +               }
465 +       }
466 +       ext3_dec_count(handle, inode);
467 +       iput(inode);
468 +       return err;
469 +}
470 +
471  static int ext3_link (struct dentry * old_dentry,
472                 struct inode * dir, struct dentry *dentry)
473  {
474 @@ -2229,7 +2247,8 @@ retry:
475         ext3_inc_count(handle, inode);
476         atomic_inc(&inode->i_count);
477  
478 -       err = ext3_add_nondir(handle, dentry, inode);
479 +       err = ext3_add_link(handle, dentry, inode);
480 +       ext3_orphan_del(handle, inode);
481         ext3_journal_stop(handle);
482         if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
483                 goto retry;
484 Index: linux-2.6.16.54-0.2.5/fs/ext3/Makefile
485 ===================================================================
486 --- linux-2.6.16.54-0.2.5.orig/fs/ext3/Makefile
487 +++ linux-2.6.16.54-0.2.5/fs/ext3/Makefile
488 @@ -4,7 +4,7 @@
489  
490  obj-$(CONFIG_EXT3_FS) += ext3.o
491  
492 -ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o \
493 +ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
494            ioctl.o namei.o super.o symlink.o hash.o resize.o
495  
496  ext3-$(CONFIG_EXT3_FS_XATTR)    += xattr.o xattr_user.o xattr_trusted.o
497 Index: linux-2.6.16.54-0.2.5/include/linux/ext3_fs.h
498 ===================================================================
499 --- linux-2.6.16.54-0.2.5.orig/include/linux/ext3_fs.h
500 +++ linux-2.6.16.54-0.2.5/include/linux/ext3_fs.h
501 @@ -375,6 +375,8 @@ struct ext3_inode {
502  #define EXT3_MOUNT_QUOTA               0x80000 /* Some quota option set */
503  #define EXT3_MOUNT_USRQUOTA            0x100000 /* "old" user quota */
504  #define EXT3_MOUNT_GRPQUOTA            0x200000 /* "old" group quota */
505 +#define EXT3_MOUNT_IOPEN               0x400000        /* Allow access via iopen */
506 +#define EXT3_MOUNT_IOPEN_NOPRIV                0x800000/* Make iopen world-readable */
507  
508  /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
509  #ifndef _LINUX_EXT2_FS_H