2 * OBDFS Super operations - also used for Lustre file system
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Copryright (C) 1999 Stelias Computing Inc. <braam@stelias.com>
7 * Copryright (C) 1999 Seagate Technology Inc.
10 #define __NO_VERSION__
12 #include <linux/locks.h>
13 #include <linux/swap.h>
15 #include <linux/obd_support.h>
16 #include <linux/obd_class.h>
17 #include <linux/obdfs.h>
20 /* XXX temporary until the real function is available from kernel
21 * XXX set this to memory size in pages for max page cache size
23 #define nr_free_buffer_pages() 32768
26 int nfract; /* Percentage of buffer cache dirty to
28 int ndirty; /* Maximum number of dirty blocks to write out per
30 int nrefill; /* Number of clean buffers to try to obtain
31 each time we call refill */
32 int nref_dirt; /* Dirty buffer threshold for activating bdflush
33 when trying to refill buffers. */
34 int interval; /* jiffies delay between pupdate flushes */
35 int age_buffer; /* Time for normal buffer to age before we flush it */
36 int age_super; /* Time for superblock to age before we flush it */
37 } pupd_prm = {40, 1024, 64, 256, 1*HZ, 30*HZ, 5*HZ };
39 /* Called with the superblock list lock held */
40 static int obdfs_enqueue_pages(struct inode *inode, struct obdo **obdo,
41 int nr_slots, struct page **pages, char **bufs,
42 obd_size *counts, obd_off *offsets,
43 obd_flag *flag, unsigned long check_time)
45 struct list_head *page_list = obdfs_iplist(inode);
46 struct list_head *tmp;
52 /* Traverse list in reverse order, so we do FIFO, not LIFO order */
53 while ( (tmp = tmp->prev) != page_list && num < nr_slots ) {
54 struct obdfs_pgrq *req;
57 req = list_entry(tmp, struct obdfs_pgrq, rq_plist);
61 if (req->rq_jiffies > check_time)
62 break; /* pages are in chronological order */
64 /* Only allocate the obdo if we will actually do I/O here */
67 *obdo = obdo_fromid(IID(inode), inode->i_ino,
69 if ( IS_ERR(*obdo) ) {
70 int err = PTR_ERR(*obdo);
77 /* FIXME revisit fromid & from_inode */
78 obdfs_from_inode(*obdo, inode);
79 *flag = OBD_BRW_CREATE;
82 /* Remove request from list before write to avoid conflict.
83 * Note that obdfs_pgrq_del() also deletes the request.
87 CDEBUG(D_CACHE, "no page \n");
91 bufs[num] = (char *)page_address(page);
93 counts[num] = PAGE_SIZE;
94 offsets[num] = ((obd_off)page->index) << PAGE_SHIFT;
95 CDEBUG(D_INFO, "ENQ inode %ld, page %p addr %p to vector\n",
96 inode->i_ino, page, (char *)page_address(page));
100 if (!list_empty(page_list))
101 CDEBUG(D_INFO, "inode %ld list not empty\n", inode->i_ino);
102 CDEBUG(D_INFO, "added %d page(s) to vector\n", num);
106 } /* obdfs_enqueue_pages */
108 /* Dequeue cached pages for a dying inode without writing them to disk. */
109 void obdfs_dequeue_pages(struct inode *inode)
111 struct list_head *tmp;
114 obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
115 tmp = obdfs_islist(inode);
116 if ( list_empty(tmp) ) {
117 CDEBUG(D_INFO, "no dirty pages for inode %ld\n", inode->i_ino);
118 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
123 /* take it out of the super list */
125 INIT_LIST_HEAD(obdfs_islist(inode));
127 tmp = obdfs_iplist(inode);
128 while ( (tmp = tmp->prev) != obdfs_iplist(inode) ) {
129 struct obdfs_pgrq *req;
132 req = list_entry(tmp, struct obdfs_pgrq, rq_plist);
134 /* take it out of the list and free */
136 /* now put the page away */
140 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
142 /* decrement inode reference for page cache */
147 /* Remove writeback requests for the superblock */
148 int obdfs_flush_reqs(struct list_head *inode_list, unsigned long check_time)
150 struct list_head *tmp;
154 struct inode *inodes[MAX_IOVEC]; /* write data back to these */
155 struct page *pages[MAX_IOVEC]; /* call put_page on these */
156 struct obdo *obdos[MAX_IOVEC];
157 char *bufs[MAX_IOVEC];
158 obd_size counts[MAX_IOVEC];
159 obd_off offsets[MAX_IOVEC];
160 obd_flag flags[MAX_IOVEC];
161 obd_count bufs_per_obdo[MAX_IOVEC];
163 struct obdfs_sb_info *sbi;
167 CDEBUG(D_INODE, "no list\n");
172 sbi = list_entry(inode_list, struct obdfs_sb_info, osi_inodes);
174 obd_down(&sbi->osi_list_mutex);
175 if ( list_empty(inode_list) ) {
176 CDEBUG(D_INFO, "list empty\n");
177 obd_up(&sbi->osi_list_mutex);
182 /* Add each inode's dirty pages to a write vector, and write it.
183 * Traverse list in reverse order, so we do FIFO, not LIFO order
189 while ( (tmp = tmp->prev) != inode_list && total_io < pupd_prm.ndirty) {
190 struct obdfs_inode_info *ii;
194 ii = list_entry(tmp, struct obdfs_inode_info, oi_inodes);
195 inode = list_entry(ii, struct inode, u);
196 inodes[num_obdos] = inode;
197 obdos[num_obdos] = NULL;
198 CDEBUG(D_INFO, "checking inode %ld pages\n", inode->i_ino);
200 /* Make sure we reference "inode" and not "inodes[num_obdos]",
201 * as num_obdos will change after the loop is run.
203 if (!list_empty(obdfs_iplist(inode))) {
204 res = obdfs_enqueue_pages(inode, &obdos[num_obdos],
206 &pages[num_io], &bufs[num_io],
211 CDEBUG(D_INFO, "FLUSH inode %ld, pages flushed: %d\n",
215 "fatal: unable to enqueue inode %ld (err %d)\n",
217 /* XXX Move bad inode to end of list so we can
218 * continue with flushing list. This is a
219 * temporary measure to avoid machine lockups.
222 list_add(tmp, inode_list);
232 bufs_per_obdo[num_obdos] = res;
235 if ( num_io == MAX_IOVEC ) {
236 obd_up(&sbi->osi_list_mutex);
237 err = obdfs_do_vec_wr(inodes, num_io, num_obdos,
238 obdos, bufs_per_obdo,
243 "fatal: do_vec_wr err=%d\n",
248 obd_down(&sbi->osi_list_mutex);
255 obd_up(&sbi->osi_list_mutex);
257 /* flush any remaining I/Os */
259 err = obdfs_do_vec_wr(inodes, num_io, num_obdos, obdos,
260 bufs_per_obdo, pages, bufs, counts,
263 CDEBUG(D_INODE, "fatal: unable to do vec_wr (err %d)\n", err);
268 /* Remove inode from superblock dirty list when no more pages.
269 * Make sure we don't point at the current inode with tmp
270 * when we re-init the list on the inode, or we will loop.
272 obd_down(&sbi->osi_list_mutex);
274 while ( (tmp = tmp->prev) != inode_list ) {
275 struct obdfs_inode_info *ii;
278 ii = list_entry(tmp, struct obdfs_inode_info, oi_inodes);
279 inode = list_entry(ii, struct inode, u);
280 CDEBUG(D_INFO, "checking inode %ld empty\n", inode->i_ino);
281 if (list_empty(obdfs_iplist(inode))) {
282 CDEBUG(D_INFO, "remove inode %ld from dirty list\n",
285 list_del(obdfs_islist(inode));
286 /* decrement inode reference for page cache */
288 INIT_LIST_HEAD(obdfs_islist(inode));
291 obd_up(&sbi->osi_list_mutex);
293 CDEBUG(D_INFO, "flushed %d pages in total\n", total_io);
296 return err ? err : total_io;
297 } /* obdfs_flush_reqs */
300 /* Walk all of the superblocks and write out blocks which are too old.
301 * Return the maximum number of blocks written for a single filesystem.
303 int obdfs_flush_dirty_pages(unsigned long check_time)
305 struct list_head *sl;
309 sl = &obdfs_super_list;
310 while ( (sl = sl->prev) != &obdfs_super_list ) {
311 struct obdfs_sb_info *sbi =
312 list_entry(sl, struct obdfs_sb_info, osi_list);
315 /* walk write requests here, use the sb, check the time */
316 ret = obdfs_flush_reqs(&sbi->osi_inodes, check_time);
317 /* XXX handle error? What to do with it? */
319 max = ret > max ? ret : max;
323 } /* obdfs_flush_dirty_pages */
326 static struct task_struct *pupdated;
328 static int pupdate(void *unused)
330 int interval = pupd_prm.interval;
331 long age = pupd_prm.age_buffer;
338 pupdated->session = 1;
340 strcpy(pupdated->comm, "pupdated");
342 printk("pupdated activated...\n");
344 spin_lock_irq(&pupdated->sigmask_lock);
345 sigfillset(&pupdated->blocked);
346 siginitsetinv(&pupdated->blocked, sigmask(SIGTERM));
347 recalc_sigpending(pupdated);
348 spin_unlock_irq(&pupdated->sigmask_lock);
353 /* update interval */
355 set_task_state(pupdated, TASK_INTERRUPTIBLE);
356 schedule_timeout(interval);
358 if (signal_pending(pupdated))
361 spin_lock_irq(&pupdated->sigmask_lock);
362 if (sigismember(&pupdated->signal, SIGTERM))
364 sigdelset(&pupdated->signal, SIGTERM);
367 recalc_sigpending(pupdated);
368 spin_unlock_irq(&pupdated->sigmask_lock);
370 printk("pupdated stopped...\n");
371 set_task_state(pupdated, TASK_STOPPED);
376 /* asynchronous setattr etc for the future ...
377 obdfs_flush_dirty_inodes(jiffies - pupd_prm.age_super);
379 dirty_limit = nr_free_buffer_pages() * pupd_prm.nfract / 100;
381 if (obdfs_cache_count > dirty_limit) {
383 if ( wrote < pupd_prm.ndirty )
385 CDEBUG(D_CACHE, "wrote %d, age %ld, interval %d\n",
386 wrote, age, interval);
388 if ( wrote < pupd_prm.ndirty >> 1 &&
389 obdfs_cache_count < dirty_limit / 2) {
390 interval = pupd_prm.interval;
391 age = pupd_prm.age_buffer;
393 "wrote %d, age %ld, interval %d\n",
394 wrote, age, interval);
395 } else if (obdfs_cache_count > dirty_limit / 2) {
397 if ( wrote < pupd_prm.ndirty )
400 "wrote %d, age %ld, interval %d\n",
401 wrote, age, interval);
405 wrote = obdfs_flush_dirty_pages(jiffies - age);
408 "dirty_limit %ld, cache_count %ld, wrote %d\n",
409 dirty_limit, obdfs_cache_count, wrote);
414 int obdfs_flushd_init(void)
417 kernel_thread(bdflush, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
419 kernel_thread(pupdate, NULL, 0);
420 CDEBUG(D_PSDEV, __FUNCTION__ ": flushd inited\n");
424 int obdfs_flushd_cleanup(void)
428 if (pupdated) /* for debugging purposes only */
429 CDEBUG(D_CACHE, "pupdated->state = %lx\n", pupdated->state);
431 /* deliver a signal to pupdated to shut it down */
432 if (pupdated && (pupdated->state == TASK_RUNNING ||
433 pupdated->state == TASK_INTERRUPTIBLE )) {
434 unsigned long timeout = HZ/20;
435 unsigned long count = 0;
436 send_sig_info(SIGTERM, (struct siginfo *)1, pupdated);
438 if ((count % 2*HZ) == timeout)
439 printk(KERN_INFO "wait for pupdated to stop\n");
441 set_current_state(TASK_INTERRUPTIBLE);
442 schedule_timeout(timeout);