Whamcloud - gitweb
file xnu_types.h was initially added on branch b_port_step.
[fs/lustre-release.git] / lustre / kernel_patches / patches / ext3-delete_thread-2.4.19-suse.patch
1  fs/ext3/file.c             |    4 
2  fs/ext3/inode.c            |  116 ++++++++++++++++++++++
3  fs/ext3/super.c            |  230 +++++++++++++++++++++++++++++++++++++++++++++
4  include/linux/ext3_fs.h    |    5 
5  include/linux/ext3_fs_sb.h |   10 +
6  5 files changed, 365 insertions(+)
7
8 Index: linux-2.4.19.SuSE/fs/ext3/super.c
9 ===================================================================
10 --- linux-2.4.19.SuSE.orig/fs/ext3/super.c      Sun Nov 16 01:18:04 2003
11 +++ linux-2.4.19.SuSE/fs/ext3/super.c   Sun Nov 16 01:19:22 2003
12 @@ -401,6 +401,220 @@
13         }
14  }
15  
16 +#ifdef EXT3_DELETE_THREAD
17 +/*
18 + * Delete inodes in a loop until there are no more to be deleted.
19 + * Normally, we run in the background doing the deletes and sleeping again,
20 + * and clients just add new inodes to be deleted onto the end of the list.
21 + * If someone is concerned about free space (e.g. block allocation or similar)
22 + * then they can sleep on s_delete_waiter_queue and be woken up when space
23 + * has been freed.
24 + */
25 +int ext3_delete_thread(void *data)
26 +{
27 +       struct super_block *sb = data;
28 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
29 +       struct task_struct *tsk = current;
30 +
31 +       /* Almost like daemonize, but not quite */
32 +       exit_mm(current);
33 +       tsk->session = 1;
34 +       tsk->pgrp = 1;
35 +       tsk->tty = NULL;
36 +       exit_files(current);
37 +       reparent_to_init();
38 +
39 +       sprintf(tsk->comm, "kdelext3-%s", kdevname(sb->s_dev));
40 +       sigfillset(&tsk->blocked);
41 +
42 +       /*tsk->flags |= PF_KERNTHREAD;*/
43 +
44 +       INIT_LIST_HEAD(&sbi->s_delete_list);
45 +       wake_up(&sbi->s_delete_waiter_queue);
46 +       ext3_debug("delete thread on %s started\n", kdevname(sb->s_dev));
47 +
48 +       /* main loop */
49 +       for (;;) {
50 +               wait_event_interruptible(sbi->s_delete_thread_queue,
51 +                                        !list_empty(&sbi->s_delete_list) ||
52 +                                        !test_opt(sb, ASYNCDEL));
53 +               ext3_debug("%s woken up: %lu inodes, %lu blocks\n",
54 +                          tsk->comm,sbi->s_delete_inodes,sbi->s_delete_blocks);
55 +
56 +               spin_lock(&sbi->s_delete_lock);
57 +               if (list_empty(&sbi->s_delete_list)) {
58 +                       clear_opt(sbi->s_mount_opt, ASYNCDEL);
59 +                       memset(&sbi->s_delete_list, 0,
60 +                              sizeof(sbi->s_delete_list));
61 +                       spin_unlock(&sbi->s_delete_lock);
62 +                       ext3_debug("delete thread on %s exiting\n",
63 +                                  kdevname(sb->s_dev));
64 +                       wake_up(&sbi->s_delete_waiter_queue);
65 +                       break;
66 +               }
67 +
68 +               while (!list_empty(&sbi->s_delete_list)) {
69 +                       struct inode *inode=list_entry(sbi->s_delete_list.next,
70 +                                                      struct inode, i_dentry);
71 +                       unsigned long blocks = inode->i_blocks >>
72 +                                                       (inode->i_blkbits - 9);
73 +
74 +                       list_del_init(&inode->i_dentry);
75 +                       spin_unlock(&sbi->s_delete_lock);
76 +                       ext3_debug("%s delete ino %lu blk %lu\n",
77 +                                  tsk->comm, inode->i_ino, blocks);
78 +
79 +                       iput(inode);
80 +
81 +                       spin_lock(&sbi->s_delete_lock);
82 +                       sbi->s_delete_blocks -= blocks;
83 +                       sbi->s_delete_inodes--;
84 +               }
85 +               if (sbi->s_delete_blocks != 0 || sbi->s_delete_inodes != 0) {
86 +                       ext3_warning(sb, __FUNCTION__,
87 +                                    "%lu blocks, %lu inodes on list?\n",
88 +                                    sbi->s_delete_blocks,sbi->s_delete_inodes);
89 +                       sbi->s_delete_blocks = 0;
90 +                       sbi->s_delete_inodes = 0;
91 +               }
92 +               spin_unlock(&sbi->s_delete_lock);
93 +               wake_up(&sbi->s_delete_waiter_queue);
94 +       }
95 +
96 +       return 0;
97 +}
98 +
99 +static void ext3_start_delete_thread(struct super_block *sb)
100 +{
101 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
102 +       int rc;
103 +
104 +       spin_lock_init(&sbi->s_delete_lock);
105 +       init_waitqueue_head(&sbi->s_delete_thread_queue);
106 +       init_waitqueue_head(&sbi->s_delete_waiter_queue);
107 +
108 +       if (!test_opt(sb, ASYNCDEL))
109 +               return;
110 +
111 +       rc = kernel_thread(ext3_delete_thread, sb, CLONE_VM | CLONE_FILES);
112 +       if (rc < 0)
113 +               printk(KERN_ERR "EXT3-fs: cannot start delete thread: rc %d\n",
114 +                      rc);
115 +       else
116 +               wait_event(sbi->s_delete_waiter_queue, sbi->s_delete_list.next);
117 +}
118 +
119 +static void ext3_stop_delete_thread(struct ext3_sb_info *sbi)
120 +{
121 +       if (sbi->s_delete_list.next == 0)       /* thread never started */
122 +               return;
123 +
124 +       clear_opt(sbi->s_mount_opt, ASYNCDEL);
125 +       wake_up(&sbi->s_delete_thread_queue);
126 +       wait_event(sbi->s_delete_waiter_queue, list_empty(&sbi->s_delete_list));
127 +}
128 +
129 +/* Instead of playing games with the inode flags, destruction, etc we just
130 + * create a new inode locally and put it on a list for the truncate thread.
131 + * We need large parts of the inode struct in order to complete the
132 + * truncate and unlink, so we may as well just have a real inode to do it.
133 + *
134 + * If we have any problem deferring the delete, just delete it right away.
135 + * If we defer it, we also mark how many blocks it would free, so that we
136 + * can keep the statfs data correct, and we know if we should sleep on the
137 + * delete thread when we run out of space.
138 + */
139 +static void ext3_delete_inode_thread(struct inode *old_inode)
140 +{
141 +       struct ext3_sb_info *sbi = EXT3_SB(old_inode->i_sb);
142 +       struct ext3_inode_info *nei, *oei = EXT3_I(old_inode);
143 +       struct inode *new_inode;
144 +       unsigned long blocks = old_inode->i_blocks >> (old_inode->i_blkbits-9);
145 +
146 +       if (is_bad_inode(old_inode)) {
147 +               clear_inode(old_inode);
148 +               return;
149 +       }
150 +
151 +       if (!test_opt(old_inode->i_sb, ASYNCDEL) || !sbi->s_delete_list.next)
152 +               goto out_delete;
153 +
154 +       /* We may want to delete the inode immediately and not defer it */
155 +       if (IS_SYNC(old_inode) || blocks <= EXT3_NDIR_BLOCKS)
156 +               goto out_delete;
157 +
158 +       /* We can't use the delete thread as-is during real orphan recovery,
159 +        * as we add to the orphan list here, causing ext3_orphan_cleanup()
160 +        * to loop endlessly.  It would be nice to do so, but needs work.
161 +        */
162 +       if (oei->i_state & EXT3_STATE_DELETE ||
163 +           sbi->s_mount_state & EXT3_ORPHAN_FS) {
164 +               ext3_debug("doing deferred inode %lu delete (%lu blocks)\n",
165 +                          old_inode->i_ino, blocks);
166 +               goto out_delete;
167 +       }
168 +
169 +       /* We can iget this inode again here, because our caller has unhashed
170 +        * old_inode, so new_inode will be in a different inode struct.
171 +        *
172 +        * We need to ensure that the i_orphan pointers in the other inodes
173 +        * point at the new inode copy instead of the old one so the orphan
174 +        * list doesn't get corrupted when the old orphan inode is freed.
175 +        */
176 +       down(&sbi->s_orphan_lock);
177 +
178 +       sbi->s_mount_state |= EXT3_ORPHAN_FS;
179 +       new_inode = iget(old_inode->i_sb, old_inode->i_ino);
180 +       sbi->s_mount_state &= ~EXT3_ORPHAN_FS;
181 +       if (is_bad_inode(new_inode)) {
182 +               printk(KERN_WARNING "read bad inode %lu\n", old_inode->i_ino);
183 +               iput(new_inode);
184 +               new_inode = NULL;
185 +       }
186 +       if (!new_inode) {
187 +               up(&sbi->s_orphan_lock);
188 +               ext3_debug("delete inode %lu directly (bad read)\n",
189 +                          old_inode->i_ino);
190 +               goto out_delete;
191 +       }
192 +       J_ASSERT(new_inode != old_inode);
193 +
194 +       J_ASSERT(!list_empty(&oei->i_orphan));
195 +
196 +       nei = EXT3_I(new_inode);
197 +       /* Ugh.  We need to insert new_inode into the same spot on the list
198 +        * as old_inode was, to ensure the in-memory orphan list is still
199 +        * in the same order as the on-disk orphan list (badness otherwise).
200 +        */
201 +       nei->i_orphan = oei->i_orphan;
202 +       nei->i_orphan.next->prev = &nei->i_orphan;
203 +       nei->i_orphan.prev->next = &nei->i_orphan;
204 +       nei->i_state |= EXT3_STATE_DELETE;
205 +       up(&sbi->s_orphan_lock);
206 +
207 +       clear_inode(old_inode);
208 +
209 +       spin_lock(&sbi->s_delete_lock);
210 +       J_ASSERT(list_empty(&new_inode->i_dentry));
211 +       list_add_tail(&new_inode->i_dentry, &sbi->s_delete_list);
212 +       sbi->s_delete_blocks += blocks;
213 +       sbi->s_delete_inodes++;
214 +       spin_unlock(&sbi->s_delete_lock);
215 +
216 +       ext3_debug("delete inode %lu (%lu blocks) by thread\n",
217 +                  new_inode->i_ino, blocks);
218 +
219 +       wake_up(&sbi->s_delete_thread_queue);
220 +       return;
221 +
222 +out_delete:
223 +       ext3_delete_inode(old_inode);
224 +}
225 +#else
226 +#define ext3_start_delete_thread(sbi) do {} while(0)
227 +#define ext3_stop_delete_thread(sbi) do {} while(0)
228 +#endif /* EXT3_DELETE_THREAD */
229 +
230  void ext3_put_super (struct super_block * sb)
231  {
232         struct ext3_sb_info *sbi = EXT3_SB(sb);
233 @@ -408,6 +622,7 @@
234         kdev_t j_dev = sbi->s_journal->j_dev;
235         int i;
236  
237 +       ext3_stop_delete_thread(sbi);
238         ext3_xattr_put_super(sb);
239         journal_destroy(sbi->s_journal);
240         if (!(sb->s_flags & MS_RDONLY)) {
241 @@ -476,7 +691,11 @@
242         write_inode:    ext3_write_inode,       /* BKL not held.  Don't need */
243         dirty_inode:    ext3_dirty_inode,       /* BKL not held.  We take it */
244         put_inode:      ext3_put_inode,         /* BKL not held.  Don't need */
245 +#ifdef EXT3_DELETE_THREAD
246 +       delete_inode:   ext3_delete_inode_thread,/* BKL not held. We take it */
247 +#else
248         delete_inode:   ext3_delete_inode,      /* BKL not held.  We take it */
249 +#endif
250         put_super:      ext3_put_super,         /* BKL held */
251         write_super:    ext3_write_super,       /* BKL held */
252         sync_fs:        ext3_sync_fs,
253 @@ -553,6 +772,13 @@
254                         clear_opt (*mount_options, POSIX_ACL);
255                 else
256  #endif
257 +#ifdef EXT3_DELETE_THREAD
258 +               if (!strcmp(this_char, "asyncdel"))
259 +                       set_opt(*mount_options, ASYNCDEL);
260 +               else if (!strcmp(this_char, "noasyncdel"))
261 +                       clear_opt(*mount_options, ASYNCDEL);
262 +               else
263 +#endif
264                 if (!strcmp (this_char, "bsddf"))
265                         clear_opt (*mount_options, MINIX_DF);
266                 else if (!strcmp (this_char, "nouid32")) {
267 @@ -1254,6 +1480,7 @@
268         }
269  
270         ext3_setup_super (sb, es, sb->s_flags & MS_RDONLY);
271 +       ext3_start_delete_thread(sb);
272         /*
273          * akpm: core read_super() calls in here with the superblock locked.
274          * That deadlocks, because orphan cleanup needs to lock the superblock
275 @@ -1692,6 +1919,9 @@
276         if (!parse_options(data, &tmp, sbi, &tmp, 1))
277                 return -EINVAL;
278  
279 +       if (!test_opt(sb, ASYNCDEL) || (*flags & MS_RDONLY))
280 +               ext3_stop_delete_thread(sbi);
281 +
282         if (sbi->s_mount_opt & EXT3_MOUNT_ABORT)
283                 ext3_abort(sb, __FUNCTION__, "Abort forced by user");
284  
285 Index: linux-2.4.19.SuSE/fs/ext3/inode.c
286 ===================================================================
287 --- linux-2.4.19.SuSE.orig/fs/ext3/inode.c      Sun Nov 16 01:02:56 2003
288 +++ linux-2.4.19.SuSE/fs/ext3/inode.c   Sun Nov 16 01:19:22 2003
289 @@ -2114,6 +2114,118 @@
290         ext3_journal_stop(handle, inode);
291  }
292  
293 +#ifdef EXT3_DELETE_THREAD
294 +/* Move blocks from to-be-truncated inode over to a new inode, and delete
295 + * that one from the delete thread instead.  This avoids a lot of latency
296 + * when truncating large files.
297 + *
298 + * If we have any problem deferring the truncate, just truncate it right away.
299 + * If we defer it, we also mark how many blocks it would free, so that we
300 + * can keep the statfs data correct, and we know if we should sleep on the
301 + * delete thread when we run out of space.
302 + */
303 +void ext3_truncate_thread(struct inode *old_inode)
304 +{
305 +       struct ext3_sb_info *sbi = EXT3_SB(old_inode->i_sb);
306 +       struct ext3_inode_info *nei, *oei = EXT3_I(old_inode);
307 +       struct inode *new_inode;
308 +       handle_t *handle;
309 +       unsigned long blocks = old_inode->i_blocks >> (old_inode->i_blkbits-9);
310 +
311 +       if (!test_opt(old_inode->i_sb, ASYNCDEL) || !sbi->s_delete_list.next)
312 +               goto out_truncate;
313 +
314 +       /* XXX This is a temporary limitation for code simplicity.
315 +        *     We could truncate to arbitrary sizes at some later time.
316 +        */
317 +       if (old_inode->i_size != 0)
318 +               goto out_truncate;
319 +
320 +       /* We may want to truncate the inode immediately and not defer it */
321 +       if (IS_SYNC(old_inode) || blocks <= EXT3_NDIR_BLOCKS ||
322 +           old_inode->i_size > oei->i_disksize)
323 +               goto out_truncate;
324 +
325 +       /* We can't use the delete thread as-is during real orphan recovery,
326 +        * as we add to the orphan list here, causing ext3_orphan_cleanup()
327 +        * to loop endlessly.  It would be nice to do so, but needs work.
328 +        */
329 +       if (oei->i_state & EXT3_STATE_DELETE ||
330 +           sbi->s_mount_state & EXT3_ORPHAN_FS) {
331 +               ext3_debug("doing deferred inode %lu delete (%lu blocks)\n",
332 +                          old_inode->i_ino, blocks);
333 +               goto out_truncate;
334 +       }
335 +
336 +       ext3_discard_prealloc(old_inode);
337 +
338 +       /* old_inode   = 1
339 +        * new_inode   = sb + GDT + ibitmap
340 +        * orphan list = 1 inode/superblock for add, 2 inodes for del
341 +        * quota files = 2 * EXT3_SINGLEDATA_TRANS_BLOCKS
342 +        */
343 +       handle = ext3_journal_start(old_inode, 7);
344 +       if (IS_ERR(handle))
345 +               goto out_truncate;
346 +
347 +       new_inode = ext3_new_inode(handle, old_inode, old_inode->i_mode);
348 +       if (IS_ERR(new_inode)) {
349 +               ext3_debug("truncate inode %lu directly (no new inodes)\n",
350 +                          old_inode->i_ino);
351 +               goto out_journal;
352 +       }
353 +
354 +       nei = EXT3_I(new_inode);
355 +
356 +       down_write(&oei->truncate_sem);
357 +       new_inode->i_size = old_inode->i_size;
358 +       new_inode->i_blocks = old_inode->i_blocks;
359 +       new_inode->i_uid = old_inode->i_uid;
360 +       new_inode->i_gid = old_inode->i_gid;
361 +       new_inode->i_nlink = 0;
362 +
363 +       /* FIXME when we do arbitrary truncates */
364 +       old_inode->i_blocks = oei->i_file_acl ? old_inode->i_blksize / 512 : 0;
365 +       old_inode->i_mtime = old_inode->i_ctime = CURRENT_TIME;
366 +
367 +       memcpy(nei->i_data, oei->i_data, sizeof(nei->i_data));
368 +       memset(oei->i_data, 0, sizeof(oei->i_data));
369 +
370 +       nei->i_disksize = oei->i_disksize;
371 +       nei->i_state |= EXT3_STATE_DELETE;
372 +       up_write(&oei->truncate_sem);
373 +
374 +       if (ext3_orphan_add(handle, new_inode) < 0)
375 +               goto out_journal;
376 +
377 +       if (ext3_orphan_del(handle, old_inode) < 0) {
378 +               ext3_orphan_del(handle, new_inode);
379 +               iput(new_inode);
380 +               goto out_journal;
381 +       }
382 +
383 +       ext3_journal_stop(handle, old_inode);
384 +
385 +       spin_lock(&sbi->s_delete_lock);
386 +       J_ASSERT(list_empty(&new_inode->i_dentry));
387 +       list_add_tail(&new_inode->i_dentry, &sbi->s_delete_list);
388 +       sbi->s_delete_blocks += blocks;
389 +       sbi->s_delete_inodes++;
390 +       spin_unlock(&sbi->s_delete_lock);
391 +
392 +       ext3_debug("delete inode %lu (%lu blocks) by thread\n",
393 +                  new_inode->i_ino, blocks);
394 +
395 +       wake_up(&sbi->s_delete_thread_queue);
396 +       return;
397 +
398 +out_journal:
399 +       ext3_journal_stop(handle, old_inode);
400 +out_truncate:
401 +       ext3_truncate(old_inode);
402 +}
403 +#endif /* EXT3_DELETE_THREAD */
404 +
405  /* 
406   * ext3_get_inode_loc returns with an extra refcount against the
407   * inode's underlying buffer_head on success. 
408 Index: linux-2.4.19.SuSE/fs/ext3/file.c
409 ===================================================================
410 --- linux-2.4.19.SuSE.orig/fs/ext3/file.c       Sun Nov 16 00:40:59 2003
411 +++ linux-2.4.19.SuSE/fs/ext3/file.c    Sun Nov 16 01:19:22 2003
412 @@ -132,7 +132,11 @@
413  };
414  
415  struct inode_operations ext3_file_inode_operations = {
416 +#ifdef EXT3_DELETE_THREAD
417 +       truncate:       ext3_truncate_thread,   /* BKL held */
418 +#else
419         truncate:       ext3_truncate,          /* BKL held */
420 +#endif
421         setattr:        ext3_setattr,           /* BKL held */
422         setxattr:       ext3_setxattr,          /* BKL held */
423         getxattr:       ext3_getxattr,          /* BKL held */
424 Index: linux-2.4.19.SuSE/include/linux/ext3_fs.h
425 ===================================================================
426 --- linux-2.4.19.SuSE.orig/include/linux/ext3_fs.h      Sun Nov 16 01:02:51 2003
427 +++ linux-2.4.19.SuSE/include/linux/ext3_fs.h   Sun Nov 16 01:20:06 2003
428 @@ -193,6 +193,7 @@
429   */
430  #define EXT3_STATE_JDATA               0x00000001 /* journaled data exists */
431  #define EXT3_STATE_NEW                 0x00000002 /* inode is newly created */
432 +#define EXT3_STATE_DELETE              0x00000010 /* deferred delete inode */
433  
434  /*
435   * ioctl commands
436 @@ -321,6 +322,7 @@
437  #define EXT3_MOUNT_NO_UID32            0x2000  /* Disable 32-bit UIDs */
438  #define EXT3_MOUNT_XATTR_USER          0x4000  /* Extended user attributes */
439  #define EXT3_MOUNT_POSIX_ACL           0x8000  /* POSIX Access Control Lists */
440 +#define EXT3_MOUNT_ASYNCDEL            0x20000 /* Delayed deletion */
441  
442  /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
443  #ifndef _LINUX_EXT2_FS_H
444 @@ -695,6 +697,9 @@
445  extern void ext3_dirty_inode(struct inode *);
446  extern int ext3_change_inode_journal_flag(struct inode *, int);
447  extern void ext3_truncate (struct inode *);
448 +#ifdef EXT3_DELETE_THREAD
449 +extern void ext3_truncate_thread(struct inode *inode);
450 +#endif
451  
452  /* ioctl.c */
453  extern int ext3_ioctl (struct inode *, struct file *, unsigned int,
454 Index: linux-2.4.19.SuSE/include/linux/ext3_fs_sb.h
455 ===================================================================
456 --- linux-2.4.19.SuSE.orig/include/linux/ext3_fs_sb.h   Sun Nov 16 01:18:41 2003
457 +++ linux-2.4.19.SuSE/include/linux/ext3_fs_sb.h        Sun Nov 16 01:19:22 2003
458 @@ -29,6 +29,8 @@
459  
460  #define EXT3_MAX_GROUP_LOADED  8
461  
462 +#define EXT3_DELETE_THREAD
463 +
464  /*
465   * third extended-fs super-block data in memory
466   */
467 @@ -75,6 +77,14 @@
468         struct timer_list turn_ro_timer;        /* For turning read-only (crash simulation) */
469         wait_queue_head_t ro_wait_queue;        /* For people waiting for the fs to go read-only */
470  #endif
471 +#ifdef EXT3_DELETE_THREAD
472 +       spinlock_t s_delete_lock;
473 +       struct list_head s_delete_list;
474 +       unsigned long s_delete_blocks;
475 +       unsigned long s_delete_inodes;
476 +       wait_queue_head_t s_delete_thread_queue;
477 +       wait_queue_head_t s_delete_waiter_queue;
478 +#endif
479  };
480  
481  #endif /* _LINUX_EXT3_FS_SB */