Whamcloud - gitweb
obdfs/*.c: fixed bug with flushing of cache of deleted obdo
[fs/lustre-release.git] / lustre / obdfs / flushd.c
1 /*
2  * OBDFS Super operations - also used for Lustre file system
3  *
4  *
5  *  Copyright (C) 1991, 1992  Linus Torvalds
6  * Copryright (C) 1999 Stelias Computing Inc. <braam@stelias.com>
7  * Copryright (C) 1999 Seagate Technology Inc.
8  *
9  */
10 #define __NO_VERSION__
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/fs.h>
14 #include <linux/malloc.h>
15 #include <linux/locks.h>
16 #include <linux/errno.h>
17 #include <linux/swap.h>
18 #include <linux/smp_lock.h>
19 #include <linux/vmalloc.h>
20 #include <linux/blkdev.h>
21 #include <linux/sysrq.h>
22 #include <linux/file.h>
23 #include <linux/init.h>
24 #include <linux/quotaops.h>
25 #include <linux/iobuf.h>
26 #include <linux/highmem.h>
27
28 #include <asm/uaccess.h>
29 #include <asm/io.h>
30 #include <asm/bitops.h>
31 #include <asm/mmu_context.h>
32
33 #include <linux/obd_support.h>
34 #include <linux/obd_class.h>
35 #include <linux/obdfs.h>
36
37
38 struct {
39         int nfract;  /* Percentage of buffer cache dirty to 
40                         activate bdflush */
41         int ndirty;  /* Maximum number of dirty blocks to write out per
42                         wake-cycle */
43         int nrefill; /* Number of clean buffers to try to obtain
44                                 each time we call refill */
45         int nref_dirt; /* Dirty buffer threshold for activating bdflush
46                           when trying to refill buffers. */
47         int interval; /* jiffies delay between kupdate flushes */
48         int age_buffer;  /* Time for normal buffer to age before we flush it */
49         int age_super;  /* Time for superblock to age before we flush it */
50 /* } pupd_prm = {40, 500, 64, 256, 5*HZ, 30*HZ, 5*HZ };  */
51 } pupd_prm = {40, 500, 64, 256, 10*HZ, 30*HZ, 5*HZ }; 
52
53 /* Called with the superblock list lock */
54 static int obdfs_enqueue_pages(struct inode *inode, struct obdo **obdo,
55                                int nr_slots, struct page **pages, char **bufs,
56                                obd_size *counts, obd_off *offsets,
57                                obd_flag *flag, int check_time)
58 {
59         struct list_head *page_list = obdfs_iplist(inode);
60         struct list_head *tmp;
61         int num = 0;
62
63         ENTRY;
64         OIDEBUG(inode);
65
66         *obdo = obdo_fromid(IID(inode), inode->i_ino, OBD_MD_FLNOTOBD);
67         if ( IS_ERR(*obdo) ) {
68                 EXIT;
69                 return PTR_ERR(*obdo);
70         }
71
72         obdfs_from_inode(*obdo, inode); /* FIXME revisit fromid & from_inode */
73         *flag = OBD_BRW_CREATE;
74
75         tmp = page_list;
76         while ( ((tmp = tmp->next) != page_list) && (num < nr_slots) ) {
77                 struct obdfs_pgrq *req;
78                 struct page *page;
79                 
80                 req = list_entry(tmp, struct obdfs_pgrq, rq_plist);
81                 page = req->rq_page;
82
83                 
84                 if (check_time && 
85                     (jiffies - req->rq_jiffies) < pupd_prm.age_buffer)
86                         continue;
87
88                 /* Remove request from list before write to avoid conflict.
89                  * Note that obdfs_pgrq_del() also deletes the request.
90                  */
91                 obdfs_pgrq_del(req);
92                 if ( !page ) {
93                         CDEBUG(D_INODE, "no page \n");
94                         continue;
95                 }
96
97                 bufs[num] = (char *)page_address(page);
98                 pages[num] = page;
99                 counts[num] = PAGE_SIZE;
100                 offsets[num] = ((obd_off)page->index) << PAGE_SHIFT;
101                 CDEBUG(D_INODE, "ENQ inode %ld, page %p addr %p to vector\n", 
102                        inode->i_ino, page, (char *)page_address(page));
103                 num++;
104         }
105
106         if (!list_empty(page_list))
107                 CDEBUG(D_INODE, "inode %ld list not empty\n", inode->i_ino);
108         CDEBUG(D_INODE, "added %d page(s) to vector\n", num);
109
110         EXIT;
111         return num;  
112 } /* obdfs_enqueue_pages */
113
114 /* Remove writeback requests for the superblock */
115 int obdfs_flush_reqs(struct list_head *inode_list, int check_time)
116 {
117         struct list_head *tmp;
118         int               total_io = 0;
119         obd_count         num_io = 0;
120         obd_count         num_obdos = 0;
121         struct inode     *inodes[MAX_IOVEC];    /* write data back to these */
122         struct page      *pages[MAX_IOVEC];     /* call put_page on these */
123         struct obdo      *obdos[MAX_IOVEC];
124         char             *bufs[MAX_IOVEC];
125         obd_size          counts[MAX_IOVEC];
126         obd_off           offsets[MAX_IOVEC];
127         obd_flag          flags[MAX_IOVEC];
128         obd_count         bufs_per_obdo[MAX_IOVEC];
129         int               err = 0;
130         struct obdfs_sb_info *sbi;
131
132         ENTRY;
133         if (!inode_list) {
134                 CDEBUG(D_INODE, "no list\n");
135                 EXIT;
136                 return 0;
137         }
138
139         sbi = list_entry(inode_list, struct obdfs_sb_info, osi_inodes);
140
141         obd_down(&sbi->osi_list_mutex);
142         if ( list_empty(inode_list)) {
143                 CDEBUG(D_INODE, "list empty\n");
144                 obd_up(&sbi->osi_list_mutex);
145                 EXIT;
146                 return 0;
147         }
148
149         /* add each inode's dirty pages to a write vector, and write it */
150  again:
151         tmp = inode_list;
152         while ( (tmp = tmp->next) != inode_list && 
153                 total_io < pupd_prm.ndirty) {
154                 struct obdfs_inode_info *ii;
155                 struct inode *inode;
156                 int res;
157
158                 ii = list_entry(tmp, struct obdfs_inode_info, oi_inodes);
159                 inode = list_entry(ii, struct inode, u);
160                 inodes[num_obdos] = inode;
161                 CDEBUG(D_INODE, "checking inode %ld pages\n", inode->i_ino);
162
163                 res = 1;
164
165                 /* Loop on this inode until we can't get more pages from it
166                  * (either no more pages, or the pages aren't old enough).
167                  * Make sure we reference "inode" and not "inodes[num_obdos]",
168                  * as num_obdos will change after the loop is run.
169                  */
170                 while (!list_empty(obdfs_iplist(inode)) && res &&
171                        total_io < pupd_prm.ndirty ) {
172                         res = obdfs_enqueue_pages(inode, &obdos[num_obdos],
173                                                   MAX_IOVEC - num_io,
174                                                   &pages[num_io], &bufs[num_io],
175                                                   &counts[num_io],
176                                                   &offsets[num_io],
177                                                   &flags[num_obdos],
178                                                   check_time);
179                         CDEBUG(D_INODE, "FLUSHED inode %ld, pages flushed: %d\n", 
180                                inode->i_ino, res);
181                         if ( res < 0 ) {
182                                 obd_up(&sbi->osi_list_mutex);
183                                 err = res;
184                                 goto ERR;
185                         }
186                         
187                         num_io += res;
188                         total_io += res;
189                         bufs_per_obdo[num_obdos] = res;
190                         num_obdos++;
191
192                         if ( num_io == MAX_IOVEC ) {
193                                 obd_up(&sbi->osi_list_mutex);
194                                 err = obdfs_do_vec_wr(inodes, num_io, num_obdos,
195                                                       obdos, bufs_per_obdo,
196                                                       pages, bufs, counts,
197                                                       offsets, flags);
198                                 if ( err ) {
199                                         EXIT;
200                                         goto ERR;
201                                 }
202                                 inodes[0] = inode;
203                                 num_io = 0;
204                                 num_obdos = 0;
205                                 obd_down(&sbi->osi_list_mutex);
206                                 goto again;
207                         }
208                 }
209         }
210
211         obd_up(&sbi->osi_list_mutex);
212
213         /* flush any remaining I/Os */
214         if ( num_io ) {
215                 err = obdfs_do_vec_wr(inodes, num_io, num_obdos, obdos,
216                                       bufs_per_obdo, pages, bufs, counts,
217                                       offsets, flags);
218         }
219
220         /* Remove inode from superblock dirty list when no more pages.
221          * Make sure we don't point at the current inode with tmp
222          * when we re-init the list on the inode, or we will loop.
223          */
224         obd_down(&sbi->osi_list_mutex);
225         tmp = inode_list;
226         while ( (tmp = tmp->next) != inode_list ) {
227                 struct obdfs_inode_info *ii;
228                 struct inode *inode;
229
230                 ii = list_entry(tmp, struct obdfs_inode_info, oi_inodes);
231                 inode = list_entry(ii, struct inode, u);
232                 CDEBUG(D_INODE, "checking inode %ld empty\n", inode->i_ino);
233                 if (list_empty(obdfs_iplist(inode))) {
234                         CDEBUG(D_INODE, "remove inode %ld from dirty list\n",
235                                inode->i_ino);
236                         tmp = tmp->prev;
237                         list_del(obdfs_islist(inode));
238                         /* decrement inode reference for page cache */
239                         inode->i_count--;
240                         INIT_LIST_HEAD(obdfs_islist(inode));
241                 }
242         }
243         obd_up(&sbi->osi_list_mutex);
244
245         CDEBUG(D_INODE, "flushed %d pages in total\n", total_io);
246         EXIT;
247 ERR:
248         return err;
249 } /* obdfs_flush_reqs */
250
251
252 void obdfs_flush_dirty_pages(int check_time)
253 {
254         struct list_head *sl;
255
256         ENTRY;
257         sl = &obdfs_super_list;
258         while ( (sl = sl->next) != &obdfs_super_list ) {
259                 struct obdfs_sb_info *sbi = 
260                         list_entry(sl, struct obdfs_sb_info, osi_list);
261
262                 /* walk write requests here, use the sb, check the time */
263                 obdfs_flush_reqs(&sbi->osi_inodes, check_time);
264         }
265         EXIT;
266 } /* obdfs_flush_dirty_pages */
267
268
269 static struct task_struct *pupdated;
270
271 static int pupdate(void *unused) 
272 {
273         struct task_struct * tsk = current;
274         int interval;
275         
276         pupdated = current;
277
278         exit_files(current);
279         exit_mm(current);
280
281         tsk->session = 1;
282         tsk->pgrp = 1;
283         sprintf(tsk->comm, "pupdated");
284         pupdated = current;
285
286         MOD_INC_USE_COUNT;      /* XXX until send_sig works */
287         printk("pupdated activated...\n");
288
289         /* sigstop and sigcont will stop and wakeup pupdate */
290         spin_lock_irq(&tsk->sigmask_lock);
291         sigfillset(&tsk->blocked);
292         siginitsetinv(&tsk->blocked, sigmask(SIGTERM));
293         recalc_sigpending(tsk);
294         spin_unlock_irq(&tsk->sigmask_lock);
295
296         for (;;) {
297                 /* update interval */
298                 interval = pupd_prm.interval;
299                 if (interval)
300                 {
301                         tsk->state = TASK_INTERRUPTIBLE;
302                         schedule_timeout(interval);
303                 }
304                 else
305                 {
306                 stop_pupdate:
307                         tsk->state = TASK_STOPPED;
308                         MOD_DEC_USE_COUNT; /* XXX until send_sig works */
309                         printk("pupdated stopped...\n");
310                         return 0;
311                 }
312                 /* check for sigstop */
313                 if (signal_pending(tsk))
314                 {
315                         int stopped = 0;
316                         spin_lock_irq(&tsk->sigmask_lock);
317                         if (sigismember(&tsk->signal, SIGTERM))
318                         {
319                                 sigdelset(&tsk->signal, SIGTERM);
320                                 stopped = 1;
321                         }
322                         recalc_sigpending(tsk);
323                         spin_unlock_irq(&tsk->sigmask_lock);
324                         if (stopped)
325                                 goto stop_pupdate;
326                 }
327                 /* asynchronous setattr etc for the future ...
328                 flush_inodes();
329                  */
330                 /* we don't currently check the time on the pages
331                 obdfs_flush_dirty_pages(1); 
332                  */
333                 obdfs_flush_dirty_pages(0); 
334         }
335 }
336
337
338 int obdfs_flushd_init(void)
339 {
340         /*
341         kernel_thread(bdflush, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
342          */
343         kernel_thread(pupdate, NULL, 0);
344         CDEBUG(D_PSDEV, __FUNCTION__ ": flushd inited\n");
345         return 0;
346 }
347
348 int obdfs_flushd_cleanup(void)
349 {
350         ENTRY;
351         /* deliver a signal to pupdated to shut it down
352            XXX need to kill it from user space for now XXX
353         if (pupdated) {
354                 send_sig_info(SIGTERM, 1, pupdated);
355         }
356          */
357
358         EXIT;
359         /* not reached */
360         return 0;
361
362 }