[GFS2] Fix unlinked file handling
[safe/jmp/linux-2.6] / fs / gfs2 / glops.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
4  *
5  * This copyrighted material is made available to anyone wishing to use,
6  * modify, copy, or redistribute it subject to the terms and conditions
7  * of the GNU General Public License v.2.
8  */
9
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/gfs2_ondisk.h>
16
17 #include "gfs2.h"
18 #include "lm_interface.h"
19 #include "incore.h"
20 #include "bmap.h"
21 #include "glock.h"
22 #include "glops.h"
23 #include "inode.h"
24 #include "log.h"
25 #include "meta_io.h"
26 #include "page.h"
27 #include "recovery.h"
28 #include "rgrp.h"
29 #include "util.h"
30
31 /**
32  * meta_go_sync - sync out the metadata for this glock
33  * @gl: the glock
34  * @flags: DIO_*
35  *
36  * Called when demoting or unlocking an EX glock.  We must flush
37  * to disk all dirty buffers/pages relating to this glock, and must not
38  * not return to caller to demote/unlock the glock until I/O is complete.
39  */
40
41 static void meta_go_sync(struct gfs2_glock *gl, int flags)
42 {
43         if (!(flags & DIO_METADATA))
44                 return;
45
46         if (test_and_clear_bit(GLF_DIRTY, &gl->gl_flags)) {
47                 gfs2_log_flush(gl->gl_sbd, gl);
48                 gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
49                 if (flags & DIO_RELEASE)
50                         gfs2_ail_empty_gl(gl);
51         }
52
53         clear_bit(GLF_SYNC, &gl->gl_flags);
54 }
55
56 /**
57  * meta_go_inval - invalidate the metadata for this glock
58  * @gl: the glock
59  * @flags:
60  *
61  */
62
63 static void meta_go_inval(struct gfs2_glock *gl, int flags)
64 {
65         if (!(flags & DIO_METADATA))
66                 return;
67
68         gfs2_meta_inval(gl);
69         gl->gl_vn++;
70 }
71
72 /**
73  * meta_go_demote_ok - Check to see if it's ok to unlock a glock
74  * @gl: the glock
75  *
76  * Returns: 1 if we have no cached data; ok to demote meta glock
77  */
78
79 static int meta_go_demote_ok(struct gfs2_glock *gl)
80 {
81         return !gl->gl_aspace->i_mapping->nrpages;
82 }
83
84 /**
85  * inode_go_xmote_th - promote/demote a glock
86  * @gl: the glock
87  * @state: the requested state
88  * @flags:
89  *
90  */
91
92 static void inode_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
93                               int flags)
94 {
95         if (gl->gl_state != LM_ST_UNLOCKED)
96                 gfs2_pte_inval(gl);
97         gfs2_glock_xmote_th(gl, state, flags);
98 }
99
100 /**
101  * inode_go_xmote_bh - After promoting/demoting a glock
102  * @gl: the glock
103  *
104  */
105
106 static void inode_go_xmote_bh(struct gfs2_glock *gl)
107 {
108         struct gfs2_holder *gh = gl->gl_req_gh;
109         struct buffer_head *bh;
110         int error;
111
112         if (gl->gl_state != LM_ST_UNLOCKED &&
113             (!gh || !(gh->gh_flags & GL_SKIP))) {
114                 error = gfs2_meta_read(gl, gl->gl_name.ln_number, DIO_START,
115                                        &bh);
116                 if (!error)
117                         brelse(bh);
118         }
119 }
120
121 /**
122  * inode_go_drop_th - unlock a glock
123  * @gl: the glock
124  *
125  * Invoked from rq_demote().
126  * Another node needs the lock in EXCLUSIVE mode, or lock (unused for too long)
127  * is being purged from our node's glock cache; we're dropping lock.
128  */
129
130 static void inode_go_drop_th(struct gfs2_glock *gl)
131 {
132         printk(KERN_INFO "drop th %p\n", gl->gl_object);
133         gfs2_pte_inval(gl);
134         gfs2_glock_drop_th(gl);
135 }
136
137 /**
138  * inode_go_sync - Sync the dirty data and/or metadata for an inode glock
139  * @gl: the glock protecting the inode
140  * @flags:
141  *
142  */
143
144 static void inode_go_sync(struct gfs2_glock *gl, int flags)
145 {
146         int meta = (flags & DIO_METADATA);
147         int data = (flags & DIO_DATA);
148
149         if (test_bit(GLF_DIRTY, &gl->gl_flags)) {
150                 if (meta && data) {
151                         printk(KERN_INFO "sync all\n");
152                         gfs2_page_sync(gl, flags | DIO_START);
153                         gfs2_log_flush(gl->gl_sbd, gl);
154                         gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
155                         gfs2_page_sync(gl, flags | DIO_WAIT);
156                         clear_bit(GLF_DIRTY, &gl->gl_flags);
157                 } else if (meta) {
158                         gfs2_log_flush(gl->gl_sbd, gl);
159                         gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
160                 } else if (data)
161                         gfs2_page_sync(gl, flags | DIO_START | DIO_WAIT);
162                 if (flags & DIO_RELEASE)
163                         gfs2_ail_empty_gl(gl);
164         }
165
166         clear_bit(GLF_SYNC, &gl->gl_flags);
167 }
168
169 /**
170  * inode_go_inval - prepare a inode glock to be released
171  * @gl: the glock
172  * @flags:
173  *
174  */
175
176 static void inode_go_inval(struct gfs2_glock *gl, int flags)
177 {
178         int meta = (flags & DIO_METADATA);
179         int data = (flags & DIO_DATA);
180
181         if (meta) {
182                 gfs2_meta_inval(gl);
183                 gl->gl_vn++;
184         }
185         if (data)
186                 gfs2_page_inval(gl);
187 }
188
189 /**
190  * inode_go_demote_ok - Check to see if it's ok to unlock an inode glock
191  * @gl: the glock
192  *
193  * Returns: 1 if it's ok
194  */
195
196 static int inode_go_demote_ok(struct gfs2_glock *gl)
197 {
198         struct gfs2_sbd *sdp = gl->gl_sbd;
199         int demote = 0;
200
201         if (!gl->gl_object && !gl->gl_aspace->i_mapping->nrpages)
202                 demote = 1;
203         else if (!sdp->sd_args.ar_localcaching &&
204                  time_after_eq(jiffies, gl->gl_stamp +
205                                gfs2_tune_get(sdp, gt_demote_secs) * HZ))
206                 demote = 1;
207
208         return demote;
209 }
210
211 /**
212  * inode_go_lock - operation done after an inode lock is locked by a process
213  * @gl: the glock
214  * @flags:
215  *
216  * Returns: errno
217  */
218
219 static int inode_go_lock(struct gfs2_holder *gh)
220 {
221         struct gfs2_glock *gl = gh->gh_gl;
222         struct gfs2_inode *ip = gl->gl_object;
223         int error = 0;
224
225         if (!ip)
226                 return 0;
227
228         if (ip->i_vn != gl->gl_vn) {
229                 printk(KERN_INFO "refresh inode %p\n", &ip->i_inode);
230                 error = gfs2_inode_refresh(ip);
231                 if (error)
232                         return error;
233                 gfs2_inode_attr_in(ip);
234         }
235
236         if ((ip->i_di.di_flags & GFS2_DIF_TRUNC_IN_PROG) &&
237             (gl->gl_state == LM_ST_EXCLUSIVE) &&
238             (gh->gh_flags & GL_LOCAL_EXCL))
239                 error = gfs2_truncatei_resume(ip);
240
241         return error;
242 }
243
244 /**
245  * inode_go_unlock - operation done before an inode lock is unlocked by a
246  *                   process
247  * @gl: the glock
248  * @flags:
249  *
250  */
251
252 static void inode_go_unlock(struct gfs2_holder *gh)
253 {
254         struct gfs2_glock *gl = gh->gh_gl;
255         struct gfs2_inode *ip = gl->gl_object;
256
257         if (ip && test_bit(GLF_DIRTY, &gl->gl_flags))
258                 gfs2_inode_attr_in(ip);
259
260         if (ip)
261                 gfs2_meta_cache_flush(ip);
262 }
263
264 /**
265  * inode_greedy -
266  * @gl: the glock
267  *
268  */
269
270 static void inode_greedy(struct gfs2_glock *gl)
271 {
272         struct gfs2_sbd *sdp = gl->gl_sbd;
273         struct gfs2_inode *ip = gl->gl_object;
274         unsigned int quantum = gfs2_tune_get(sdp, gt_greedy_quantum);
275         unsigned int max = gfs2_tune_get(sdp, gt_greedy_max);
276         unsigned int new_time;
277
278         spin_lock(&ip->i_spin);
279
280         if (time_after(ip->i_last_pfault + quantum, jiffies)) {
281                 new_time = ip->i_greedy + quantum;
282                 if (new_time > max)
283                         new_time = max;
284         } else {
285                 new_time = ip->i_greedy - quantum;
286                 if (!new_time || new_time > max)
287                         new_time = 1;
288         }
289
290         ip->i_greedy = new_time;
291
292         spin_unlock(&ip->i_spin);
293
294         iput(&ip->i_inode);
295 }
296
297 /**
298  * rgrp_go_demote_ok - Check to see if it's ok to unlock a RG's glock
299  * @gl: the glock
300  *
301  * Returns: 1 if it's ok
302  */
303
304 static int rgrp_go_demote_ok(struct gfs2_glock *gl)
305 {
306         return !gl->gl_aspace->i_mapping->nrpages;
307 }
308
309 /**
310  * rgrp_go_lock - operation done after an rgrp lock is locked by
311  *    a first holder on this node.
312  * @gl: the glock
313  * @flags:
314  *
315  * Returns: errno
316  */
317
318 static int rgrp_go_lock(struct gfs2_holder *gh)
319 {
320         return gfs2_rgrp_bh_get(gh->gh_gl->gl_object);
321 }
322
323 /**
324  * rgrp_go_unlock - operation done before an rgrp lock is unlocked by
325  *    a last holder on this node.
326  * @gl: the glock
327  * @flags:
328  *
329  */
330
331 static void rgrp_go_unlock(struct gfs2_holder *gh)
332 {
333         gfs2_rgrp_bh_put(gh->gh_gl->gl_object);
334 }
335
336 /**
337  * trans_go_xmote_th - promote/demote the transaction glock
338  * @gl: the glock
339  * @state: the requested state
340  * @flags:
341  *
342  */
343
344 static void trans_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
345                               int flags)
346 {
347         struct gfs2_sbd *sdp = gl->gl_sbd;
348
349         if (gl->gl_state != LM_ST_UNLOCKED &&
350             test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
351                 gfs2_meta_syncfs(sdp);
352                 gfs2_log_shutdown(sdp);
353         }
354
355         gfs2_glock_xmote_th(gl, state, flags);
356 }
357
358 /**
359  * trans_go_xmote_bh - After promoting/demoting the transaction glock
360  * @gl: the glock
361  *
362  */
363
364 static void trans_go_xmote_bh(struct gfs2_glock *gl)
365 {
366         struct gfs2_sbd *sdp = gl->gl_sbd;
367         struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
368         struct gfs2_glock *j_gl = ip->i_gl;
369         struct gfs2_log_header head;
370         int error;
371
372         if (gl->gl_state != LM_ST_UNLOCKED &&
373             test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
374                 gfs2_meta_cache_flush(GFS2_I(sdp->sd_jdesc->jd_inode));
375                 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA | DIO_DATA);
376
377                 error = gfs2_find_jhead(sdp->sd_jdesc, &head);
378                 if (error)
379                         gfs2_consist(sdp);
380                 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT))
381                         gfs2_consist(sdp);
382
383                 /*  Initialize some head of the log stuff  */
384                 if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) {
385                         sdp->sd_log_sequence = head.lh_sequence + 1;
386                         gfs2_log_pointers_init(sdp, head.lh_blkno);
387                 }
388         }
389 }
390
391 /**
392  * trans_go_drop_th - unlock the transaction glock
393  * @gl: the glock
394  *
395  * We want to sync the device even with localcaching.  Remember
396  * that localcaching journal replay only marks buffers dirty.
397  */
398
399 static void trans_go_drop_th(struct gfs2_glock *gl)
400 {
401         struct gfs2_sbd *sdp = gl->gl_sbd;
402
403         if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
404                 gfs2_meta_syncfs(sdp);
405                 gfs2_log_shutdown(sdp);
406         }
407
408         gfs2_glock_drop_th(gl);
409 }
410
411 /**
412  * quota_go_demote_ok - Check to see if it's ok to unlock a quota glock
413  * @gl: the glock
414  *
415  * Returns: 1 if it's ok
416  */
417
418 static int quota_go_demote_ok(struct gfs2_glock *gl)
419 {
420         return !atomic_read(&gl->gl_lvb_count);
421 }
422
423 struct gfs2_glock_operations gfs2_meta_glops = {
424         .go_xmote_th = gfs2_glock_xmote_th,
425         .go_drop_th = gfs2_glock_drop_th,
426         .go_sync = meta_go_sync,
427         .go_inval = meta_go_inval,
428         .go_demote_ok = meta_go_demote_ok,
429         .go_type = LM_TYPE_META
430 };
431
432 struct gfs2_glock_operations gfs2_inode_glops = {
433         .go_xmote_th = inode_go_xmote_th,
434         .go_xmote_bh = inode_go_xmote_bh,
435         .go_drop_th = inode_go_drop_th,
436         .go_sync = inode_go_sync,
437         .go_inval = inode_go_inval,
438         .go_demote_ok = inode_go_demote_ok,
439         .go_lock = inode_go_lock,
440         .go_unlock = inode_go_unlock,
441         .go_greedy = inode_greedy,
442         .go_type = LM_TYPE_INODE
443 };
444
445 struct gfs2_glock_operations gfs2_rgrp_glops = {
446         .go_xmote_th = gfs2_glock_xmote_th,
447         .go_drop_th = gfs2_glock_drop_th,
448         .go_sync = meta_go_sync,
449         .go_inval = meta_go_inval,
450         .go_demote_ok = rgrp_go_demote_ok,
451         .go_lock = rgrp_go_lock,
452         .go_unlock = rgrp_go_unlock,
453         .go_type = LM_TYPE_RGRP
454 };
455
456 struct gfs2_glock_operations gfs2_trans_glops = {
457         .go_xmote_th = trans_go_xmote_th,
458         .go_xmote_bh = trans_go_xmote_bh,
459         .go_drop_th = trans_go_drop_th,
460         .go_type = LM_TYPE_NONDISK
461 };
462
463 struct gfs2_glock_operations gfs2_iopen_glops = {
464         .go_xmote_th = gfs2_glock_xmote_th,
465         .go_drop_th = gfs2_glock_drop_th,
466         .go_callback = gfs2_iopen_go_callback,
467         .go_type = LM_TYPE_IOPEN
468 };
469
470 struct gfs2_glock_operations gfs2_flock_glops = {
471         .go_xmote_th = gfs2_glock_xmote_th,
472         .go_drop_th = gfs2_glock_drop_th,
473         .go_type = LM_TYPE_FLOCK
474 };
475
476 struct gfs2_glock_operations gfs2_nondisk_glops = {
477         .go_xmote_th = gfs2_glock_xmote_th,
478         .go_drop_th = gfs2_glock_drop_th,
479         .go_type = LM_TYPE_NONDISK
480 };
481
482 struct gfs2_glock_operations gfs2_quota_glops = {
483         .go_xmote_th = gfs2_glock_xmote_th,
484         .go_drop_th = gfs2_glock_drop_th,
485         .go_demote_ok = quota_go_demote_ok,
486         .go_type = LM_TYPE_QUOTA
487 };
488
489 struct gfs2_glock_operations gfs2_journal_glops = {
490         .go_xmote_th = gfs2_glock_xmote_th,
491         .go_drop_th = gfs2_glock_drop_th,
492         .go_type = LM_TYPE_JOURNAL
493 };
494