]>
Commit | Line | Data |
---|---|---|
7336d0e6 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
b3b94faa DT |
2 | /* |
3 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. | |
da6dd40d | 4 | * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved. |
b3b94faa DT |
5 | */ |
6 | ||
d77d1b58 JP |
7 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
8 | ||
9e6e0a12 | 9 | #include <linux/bio.h> |
174cd4b1 | 10 | #include <linux/sched/signal.h> |
b3b94faa DT |
11 | #include <linux/slab.h> |
12 | #include <linux/spinlock.h> | |
13 | #include <linux/completion.h> | |
14 | #include <linux/buffer_head.h> | |
9e6e0a12 SW |
15 | #include <linux/statfs.h> |
16 | #include <linux/seq_file.h> | |
17 | #include <linux/mount.h> | |
18 | #include <linux/kthread.h> | |
19 | #include <linux/delay.h> | |
5c676f6d | 20 | #include <linux/gfs2_ondisk.h> |
9e6e0a12 SW |
21 | #include <linux/crc32.h> |
22 | #include <linux/time.h> | |
e402746a | 23 | #include <linux/wait.h> |
a9185b41 | 24 | #include <linux/writeback.h> |
4667a0ec | 25 | #include <linux/backing-dev.h> |
2e60d768 | 26 | #include <linux/kernel.h> |
b3b94faa DT |
27 | |
28 | #include "gfs2.h" | |
5c676f6d | 29 | #include "incore.h" |
b3b94faa DT |
30 | #include "bmap.h" |
31 | #include "dir.h" | |
b3b94faa DT |
32 | #include "glock.h" |
33 | #include "glops.h" | |
34 | #include "inode.h" | |
35 | #include "log.h" | |
36 | #include "meta_io.h" | |
37 | #include "quota.h" | |
38 | #include "recovery.h" | |
39 | #include "rgrp.h" | |
40 | #include "super.h" | |
41 | #include "trans.h" | |
5c676f6d | 42 | #include "util.h" |
9e6e0a12 | 43 | #include "sys.h" |
307cf6e6 | 44 | #include "xattr.h" |
f4686c26 | 45 | #include "lops.h" |
9e6e0a12 | 46 | |
53dbc27e BP |
47 | enum dinode_demise { |
48 | SHOULD_DELETE_DINODE, | |
49 | SHOULD_NOT_DELETE_DINODE, | |
50 | SHOULD_DEFER_EVICTION, | |
51 | }; | |
52 | ||
fefc03bf SW |
53 | /** |
54 | * gfs2_jindex_free - Clear all the journal index information | |
55 | * @sdp: The GFS2 superblock | |
56 | * | |
57 | */ | |
58 | ||
59 | void gfs2_jindex_free(struct gfs2_sbd *sdp) | |
60 | { | |
b50f227b | 61 | struct list_head list; |
fefc03bf | 62 | struct gfs2_jdesc *jd; |
fefc03bf SW |
63 | |
64 | spin_lock(&sdp->sd_jindex_spin); | |
65 | list_add(&list, &sdp->sd_jindex_list); | |
66 | list_del_init(&sdp->sd_jindex_list); | |
67 | sdp->sd_journals = 0; | |
68 | spin_unlock(&sdp->sd_jindex_spin); | |
69 | ||
601ef0d5 | 70 | sdp->sd_jdesc = NULL; |
fefc03bf | 71 | while (!list_empty(&list)) { |
969183bc | 72 | jd = list_first_entry(&list, struct gfs2_jdesc, jd_list); |
b50f227b | 73 | gfs2_free_journal_extents(jd); |
fefc03bf SW |
74 | list_del(&jd->jd_list); |
75 | iput(jd->jd_inode); | |
601ef0d5 | 76 | jd->jd_inode = NULL; |
fefc03bf SW |
77 | kfree(jd); |
78 | } | |
79 | } | |
80 | ||
b3b94faa DT |
81 | static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid) |
82 | { | |
83 | struct gfs2_jdesc *jd; | |
84 | int found = 0; | |
85 | ||
86 | list_for_each_entry(jd, head, jd_list) { | |
87 | if (jd->jd_jid == jid) { | |
88 | found = 1; | |
89 | break; | |
90 | } | |
91 | } | |
92 | ||
93 | if (!found) | |
94 | jd = NULL; | |
95 | ||
96 | return jd; | |
97 | } | |
98 | ||
99 | struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid) | |
100 | { | |
101 | struct gfs2_jdesc *jd; | |
102 | ||
103 | spin_lock(&sdp->sd_jindex_spin); | |
104 | jd = jdesc_find_i(&sdp->sd_jindex_list, jid); | |
105 | spin_unlock(&sdp->sd_jindex_spin); | |
106 | ||
107 | return jd; | |
108 | } | |
109 | ||
b3b94faa DT |
110 | int gfs2_jdesc_check(struct gfs2_jdesc *jd) |
111 | { | |
feaa7bba SW |
112 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
113 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); | |
a2e0f799 | 114 | u64 size = i_size_read(jd->jd_inode); |
b3b94faa | 115 | |
47a9a527 | 116 | if (gfs2_check_internal_file_size(jd->jd_inode, 8 << 20, BIT(30))) |
b3b94faa | 117 | return -EIO; |
b3b94faa | 118 | |
a2e0f799 SW |
119 | jd->jd_blocks = size >> sdp->sd_sb.sb_bsize_shift; |
120 | ||
121 | if (gfs2_write_alloc_required(ip, 0, size)) { | |
b3b94faa | 122 | gfs2_consist_inode(ip); |
461cb419 | 123 | return -EIO; |
b3b94faa DT |
124 | } |
125 | ||
461cb419 | 126 | return 0; |
b3b94faa DT |
127 | } |
128 | ||
8ad151c2 SW |
129 | static int init_threads(struct gfs2_sbd *sdp) |
130 | { | |
131 | struct task_struct *p; | |
132 | int error = 0; | |
133 | ||
134 | p = kthread_run(gfs2_logd, sdp, "gfs2_logd"); | |
135 | if (IS_ERR(p)) { | |
136 | error = PTR_ERR(p); | |
137 | fs_err(sdp, "can't start logd thread: %d\n", error); | |
138 | return error; | |
139 | } | |
140 | sdp->sd_logd_process = p; | |
141 | ||
142 | p = kthread_run(gfs2_quotad, sdp, "gfs2_quotad"); | |
143 | if (IS_ERR(p)) { | |
144 | error = PTR_ERR(p); | |
145 | fs_err(sdp, "can't start quotad thread: %d\n", error); | |
146 | goto fail; | |
147 | } | |
148 | sdp->sd_quotad_process = p; | |
149 | return 0; | |
150 | ||
151 | fail: | |
152 | kthread_stop(sdp->sd_logd_process); | |
5b3a9f34 | 153 | sdp->sd_logd_process = NULL; |
8ad151c2 SW |
154 | return error; |
155 | } | |
156 | ||
b3b94faa DT |
157 | /** |
158 | * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one | |
159 | * @sdp: the filesystem | |
160 | * | |
161 | * Returns: errno | |
162 | */ | |
163 | ||
164 | int gfs2_make_fs_rw(struct gfs2_sbd *sdp) | |
165 | { | |
feaa7bba | 166 | struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode); |
5c676f6d | 167 | struct gfs2_glock *j_gl = ip->i_gl; |
2e60d768 | 168 | struct gfs2_holder freeze_gh; |
55167622 | 169 | struct gfs2_log_header_host head; |
b3b94faa DT |
170 | int error; |
171 | ||
8ad151c2 | 172 | error = init_threads(sdp); |
b3b94faa DT |
173 | if (error) |
174 | return error; | |
175 | ||
c860f8ff BP |
176 | error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, |
177 | LM_FLAG_NOEXP | GL_EXACT, | |
2e60d768 | 178 | &freeze_gh); |
8ad151c2 SW |
179 | if (error) |
180 | goto fail_threads; | |
181 | ||
1a14d3a6 | 182 | j_gl->gl_ops->go_inval(j_gl, DIO_METADATA); |
601ef0d5 BP |
183 | if (gfs2_withdrawn(sdp)) { |
184 | error = -EIO; | |
185 | goto fail; | |
186 | } | |
b3b94faa | 187 | |
f4686c26 | 188 | error = gfs2_find_jhead(sdp->sd_jdesc, &head, false); |
601ef0d5 | 189 | if (error || gfs2_withdrawn(sdp)) |
b3b94faa DT |
190 | goto fail; |
191 | ||
192 | if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) { | |
193 | gfs2_consist(sdp); | |
194 | error = -EIO; | |
195 | goto fail; | |
196 | } | |
197 | ||
198 | /* Initialize some head of the log stuff */ | |
199 | sdp->sd_log_sequence = head.lh_sequence + 1; | |
200 | gfs2_log_pointers_init(sdp, head.lh_blkno); | |
201 | ||
b3b94faa | 202 | error = gfs2_quota_init(sdp); |
601ef0d5 | 203 | if (error || gfs2_withdrawn(sdp)) |
a91ea69f | 204 | goto fail; |
b3b94faa DT |
205 | |
206 | set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags); | |
207 | ||
2e60d768 | 208 | gfs2_glock_dq_uninit(&freeze_gh); |
b3b94faa DT |
209 | |
210 | return 0; | |
211 | ||
a91ea69f | 212 | fail: |
2e60d768 | 213 | gfs2_glock_dq_uninit(&freeze_gh); |
8ad151c2 | 214 | fail_threads: |
5b3a9f34 BP |
215 | if (sdp->sd_quotad_process) |
216 | kthread_stop(sdp->sd_quotad_process); | |
217 | sdp->sd_quotad_process = NULL; | |
218 | if (sdp->sd_logd_process) | |
219 | kthread_stop(sdp->sd_logd_process); | |
220 | sdp->sd_logd_process = NULL; | |
b3b94faa DT |
221 | return error; |
222 | } | |
223 | ||
1946f70a | 224 | void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf) |
bb8d8a6f SW |
225 | { |
226 | const struct gfs2_statfs_change *str = buf; | |
227 | ||
228 | sc->sc_total = be64_to_cpu(str->sc_total); | |
229 | sc->sc_free = be64_to_cpu(str->sc_free); | |
230 | sc->sc_dinodes = be64_to_cpu(str->sc_dinodes); | |
231 | } | |
232 | ||
73092698 | 233 | void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf) |
bb8d8a6f SW |
234 | { |
235 | struct gfs2_statfs_change *str = buf; | |
236 | ||
237 | str->sc_total = cpu_to_be64(sc->sc_total); | |
238 | str->sc_free = cpu_to_be64(sc->sc_free); | |
239 | str->sc_dinodes = cpu_to_be64(sc->sc_dinodes); | |
240 | } | |
241 | ||
b3b94faa DT |
242 | int gfs2_statfs_init(struct gfs2_sbd *sdp) |
243 | { | |
feaa7bba | 244 | struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode); |
bd209cc0 | 245 | struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master; |
feaa7bba | 246 | struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode); |
bd209cc0 | 247 | struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local; |
b3b94faa DT |
248 | struct buffer_head *m_bh, *l_bh; |
249 | struct gfs2_holder gh; | |
250 | int error; | |
251 | ||
252 | error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE, | |
253 | &gh); | |
254 | if (error) | |
255 | return error; | |
256 | ||
257 | error = gfs2_meta_inode_buffer(m_ip, &m_bh); | |
258 | if (error) | |
259 | goto out; | |
260 | ||
261 | if (sdp->sd_args.ar_spectator) { | |
262 | spin_lock(&sdp->sd_statfs_spin); | |
263 | gfs2_statfs_change_in(m_sc, m_bh->b_data + | |
264 | sizeof(struct gfs2_dinode)); | |
265 | spin_unlock(&sdp->sd_statfs_spin); | |
266 | } else { | |
267 | error = gfs2_meta_inode_buffer(l_ip, &l_bh); | |
268 | if (error) | |
269 | goto out_m_bh; | |
270 | ||
271 | spin_lock(&sdp->sd_statfs_spin); | |
272 | gfs2_statfs_change_in(m_sc, m_bh->b_data + | |
273 | sizeof(struct gfs2_dinode)); | |
274 | gfs2_statfs_change_in(l_sc, l_bh->b_data + | |
275 | sizeof(struct gfs2_dinode)); | |
276 | spin_unlock(&sdp->sd_statfs_spin); | |
277 | ||
278 | brelse(l_bh); | |
279 | } | |
280 | ||
a91ea69f | 281 | out_m_bh: |
b3b94faa | 282 | brelse(m_bh); |
a91ea69f | 283 | out: |
b3b94faa | 284 | gfs2_glock_dq_uninit(&gh); |
b3b94faa DT |
285 | return 0; |
286 | } | |
287 | ||
cd915493 SW |
288 | void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free, |
289 | s64 dinodes) | |
b3b94faa | 290 | { |
feaa7bba | 291 | struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode); |
bd209cc0 | 292 | struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local; |
3d3c10f2 | 293 | struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master; |
b3b94faa | 294 | struct buffer_head *l_bh; |
c14f5735 BM |
295 | s64 x, y; |
296 | int need_sync = 0; | |
b3b94faa DT |
297 | int error; |
298 | ||
299 | error = gfs2_meta_inode_buffer(l_ip, &l_bh); | |
300 | if (error) | |
301 | return; | |
302 | ||
350a9b0a | 303 | gfs2_trans_add_meta(l_ip->i_gl, l_bh); |
b3b94faa DT |
304 | |
305 | spin_lock(&sdp->sd_statfs_spin); | |
306 | l_sc->sc_total += total; | |
307 | l_sc->sc_free += free; | |
308 | l_sc->sc_dinodes += dinodes; | |
907b9bce | 309 | gfs2_statfs_change_out(l_sc, l_bh->b_data + sizeof(struct gfs2_dinode)); |
c14f5735 BM |
310 | if (sdp->sd_args.ar_statfs_percent) { |
311 | x = 100 * l_sc->sc_free; | |
312 | y = m_sc->sc_free * sdp->sd_args.ar_statfs_percent; | |
313 | if (x >= y || x <= -y) | |
314 | need_sync = 1; | |
315 | } | |
b3b94faa DT |
316 | spin_unlock(&sdp->sd_statfs_spin); |
317 | ||
318 | brelse(l_bh); | |
c14f5735 | 319 | if (need_sync) |
3d3c10f2 | 320 | gfs2_wake_up_statfs(sdp); |
b3b94faa DT |
321 | } |
322 | ||
1946f70a BM |
323 | void update_statfs(struct gfs2_sbd *sdp, struct buffer_head *m_bh, |
324 | struct buffer_head *l_bh) | |
325 | { | |
326 | struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode); | |
327 | struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode); | |
328 | struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master; | |
329 | struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local; | |
330 | ||
350a9b0a | 331 | gfs2_trans_add_meta(l_ip->i_gl, l_bh); |
901c6c66 | 332 | gfs2_trans_add_meta(m_ip->i_gl, m_bh); |
1946f70a BM |
333 | |
334 | spin_lock(&sdp->sd_statfs_spin); | |
335 | m_sc->sc_total += l_sc->sc_total; | |
336 | m_sc->sc_free += l_sc->sc_free; | |
337 | m_sc->sc_dinodes += l_sc->sc_dinodes; | |
338 | memset(l_sc, 0, sizeof(struct gfs2_statfs_change)); | |
339 | memset(l_bh->b_data + sizeof(struct gfs2_dinode), | |
340 | 0, sizeof(struct gfs2_statfs_change)); | |
1946f70a | 341 | gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode)); |
901c6c66 | 342 | spin_unlock(&sdp->sd_statfs_spin); |
1946f70a BM |
343 | } |
344 | ||
8c42d637 | 345 | int gfs2_statfs_sync(struct super_block *sb, int type) |
b3b94faa | 346 | { |
8c42d637 | 347 | struct gfs2_sbd *sdp = sb->s_fs_info; |
feaa7bba SW |
348 | struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode); |
349 | struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode); | |
bd209cc0 AV |
350 | struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master; |
351 | struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local; | |
b3b94faa DT |
352 | struct gfs2_holder gh; |
353 | struct buffer_head *m_bh, *l_bh; | |
354 | int error; | |
355 | ||
356 | error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE, | |
357 | &gh); | |
358 | if (error) | |
2e60d768 | 359 | goto out; |
b3b94faa DT |
360 | |
361 | error = gfs2_meta_inode_buffer(m_ip, &m_bh); | |
362 | if (error) | |
2e60d768 | 363 | goto out_unlock; |
b3b94faa DT |
364 | |
365 | spin_lock(&sdp->sd_statfs_spin); | |
366 | gfs2_statfs_change_in(m_sc, m_bh->b_data + | |
907b9bce | 367 | sizeof(struct gfs2_dinode)); |
b3b94faa DT |
368 | if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) { |
369 | spin_unlock(&sdp->sd_statfs_spin); | |
370 | goto out_bh; | |
371 | } | |
372 | spin_unlock(&sdp->sd_statfs_spin); | |
373 | ||
374 | error = gfs2_meta_inode_buffer(l_ip, &l_bh); | |
375 | if (error) | |
376 | goto out_bh; | |
377 | ||
378 | error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0); | |
379 | if (error) | |
380 | goto out_bh2; | |
381 | ||
1946f70a | 382 | update_statfs(sdp, m_bh, l_bh); |
3d3c10f2 | 383 | sdp->sd_statfs_force_sync = 0; |
b3b94faa DT |
384 | |
385 | gfs2_trans_end(sdp); | |
386 | ||
a91ea69f | 387 | out_bh2: |
b3b94faa | 388 | brelse(l_bh); |
a91ea69f | 389 | out_bh: |
b3b94faa | 390 | brelse(m_bh); |
2e60d768 | 391 | out_unlock: |
b3b94faa | 392 | gfs2_glock_dq_uninit(&gh); |
2e60d768 | 393 | out: |
b3b94faa DT |
394 | return error; |
395 | } | |
396 | ||
b3b94faa DT |
397 | struct lfcc { |
398 | struct list_head list; | |
399 | struct gfs2_holder gh; | |
400 | }; | |
401 | ||
402 | /** | |
403 | * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all | |
404 | * journals are clean | |
405 | * @sdp: the file system | |
406 | * @state: the state to put the transaction lock into | |
407 | * @t_gh: the hold on the transaction lock | |
408 | * | |
409 | * Returns: errno | |
410 | */ | |
411 | ||
52b1cdcb | 412 | static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp) |
b3b94faa | 413 | { |
5c676f6d | 414 | struct gfs2_inode *ip; |
b3b94faa DT |
415 | struct gfs2_jdesc *jd; |
416 | struct lfcc *lfcc; | |
417 | LIST_HEAD(list); | |
55167622 | 418 | struct gfs2_log_header_host lh; |
b3b94faa DT |
419 | int error; |
420 | ||
b3b94faa DT |
421 | list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) { |
422 | lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL); | |
423 | if (!lfcc) { | |
424 | error = -ENOMEM; | |
425 | goto out; | |
426 | } | |
feaa7bba SW |
427 | ip = GFS2_I(jd->jd_inode); |
428 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh); | |
b3b94faa DT |
429 | if (error) { |
430 | kfree(lfcc); | |
431 | goto out; | |
432 | } | |
433 | list_add(&lfcc->list, &list); | |
434 | } | |
435 | ||
24972557 | 436 | error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_EXCLUSIVE, |
c860f8ff | 437 | LM_FLAG_NOEXP, &sdp->sd_freeze_gh); |
52b1cdcb BP |
438 | if (error) |
439 | goto out; | |
b3b94faa DT |
440 | |
441 | list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) { | |
442 | error = gfs2_jdesc_check(jd); | |
443 | if (error) | |
444 | break; | |
f4686c26 | 445 | error = gfs2_find_jhead(jd, &lh, false); |
b3b94faa DT |
446 | if (error) |
447 | break; | |
448 | if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) { | |
449 | error = -EBUSY; | |
450 | break; | |
451 | } | |
452 | } | |
453 | ||
454 | if (error) | |
52b1cdcb | 455 | gfs2_glock_dq_uninit(&sdp->sd_freeze_gh); |
b3b94faa | 456 | |
a91ea69f | 457 | out: |
b3b94faa | 458 | while (!list_empty(&list)) { |
969183bc | 459 | lfcc = list_first_entry(&list, struct lfcc, list); |
b3b94faa DT |
460 | list_del(&lfcc->list); |
461 | gfs2_glock_dq_uninit(&lfcc->gh); | |
462 | kfree(lfcc); | |
463 | } | |
b3b94faa DT |
464 | return error; |
465 | } | |
466 | ||
9eed04cd SW |
467 | void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf) |
468 | { | |
469 | struct gfs2_dinode *str = buf; | |
470 | ||
471 | str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC); | |
472 | str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI); | |
473 | str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI); | |
474 | str->di_num.no_addr = cpu_to_be64(ip->i_no_addr); | |
475 | str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino); | |
476 | str->di_mode = cpu_to_be32(ip->i_inode.i_mode); | |
d0546426 EB |
477 | str->di_uid = cpu_to_be32(i_uid_read(&ip->i_inode)); |
478 | str->di_gid = cpu_to_be32(i_gid_read(&ip->i_inode)); | |
9eed04cd SW |
479 | str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink); |
480 | str->di_size = cpu_to_be64(i_size_read(&ip->i_inode)); | |
481 | str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode)); | |
482 | str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec); | |
483 | str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec); | |
484 | str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec); | |
485 | ||
486 | str->di_goal_meta = cpu_to_be64(ip->i_goal); | |
487 | str->di_goal_data = cpu_to_be64(ip->i_goal); | |
488 | str->di_generation = cpu_to_be64(ip->i_generation); | |
489 | ||
490 | str->di_flags = cpu_to_be32(ip->i_diskflags); | |
491 | str->di_height = cpu_to_be16(ip->i_height); | |
492 | str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) && | |
493 | !(ip->i_diskflags & GFS2_DIF_EXHASH) ? | |
494 | GFS2_FORMAT_DE : 0); | |
495 | str->di_depth = cpu_to_be16(ip->i_depth); | |
496 | str->di_entries = cpu_to_be32(ip->i_entries); | |
497 | ||
498 | str->di_eattr = cpu_to_be64(ip->i_eattr); | |
499 | str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec); | |
500 | str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec); | |
501 | str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec); | |
502 | } | |
9e6e0a12 SW |
503 | |
504 | /** | |
505 | * gfs2_write_inode - Make sure the inode is stable on the disk | |
506 | * @inode: The inode | |
1027efaa | 507 | * @wbc: The writeback control structure |
9e6e0a12 SW |
508 | * |
509 | * Returns: errno | |
510 | */ | |
511 | ||
a9185b41 | 512 | static int gfs2_write_inode(struct inode *inode, struct writeback_control *wbc) |
9e6e0a12 SW |
513 | { |
514 | struct gfs2_inode *ip = GFS2_I(inode); | |
515 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
1027efaa | 516 | struct address_space *metamapping = gfs2_glock2aspace(ip->i_gl); |
de1414a6 | 517 | struct backing_dev_info *bdi = inode_to_bdi(metamapping->host); |
ab9bbda0 | 518 | int ret = 0; |
adbc3ddf | 519 | bool flush_all = (wbc->sync_mode == WB_SYNC_ALL || gfs2_is_jdata(ip)); |
ab9bbda0 | 520 | |
adbc3ddf | 521 | if (flush_all) |
c1696fb8 | 522 | gfs2_log_flush(GFS2_SB(inode), ip->i_gl, |
805c0907 BP |
523 | GFS2_LOG_HEAD_FLUSH_NORMAL | |
524 | GFS2_LFC_WRITE_INODE); | |
a88a341a | 525 | if (bdi->wb.dirty_exceeded) |
4667a0ec | 526 | gfs2_ail1_flush(sdp, wbc); |
1d4ec642 SW |
527 | else |
528 | filemap_fdatawrite(metamapping); | |
adbc3ddf | 529 | if (flush_all) |
1027efaa SW |
530 | ret = filemap_fdatawait(metamapping); |
531 | if (ret) | |
532 | mark_inode_dirty_sync(inode); | |
957a7acd AD |
533 | else { |
534 | spin_lock(&inode->i_lock); | |
535 | if (!(inode->i_flags & I_DIRTY)) | |
536 | gfs2_ordered_del_inode(ip); | |
537 | spin_unlock(&inode->i_lock); | |
538 | } | |
9e6e0a12 SW |
539 | return ret; |
540 | } | |
541 | ||
ab9bbda0 SW |
542 | /** |
543 | * gfs2_dirty_inode - check for atime updates | |
544 | * @inode: The inode in question | |
545 | * @flags: The type of dirty | |
546 | * | |
547 | * Unfortunately it can be called under any combination of inode | |
548 | * glock and transaction lock, so we have to check carefully. | |
549 | * | |
550 | * At the moment this deals only with atime - it should be possible | |
551 | * to expand that role in future, once a review of the locking has | |
552 | * been carried out. | |
553 | */ | |
554 | ||
555 | static void gfs2_dirty_inode(struct inode *inode, int flags) | |
556 | { | |
557 | struct gfs2_inode *ip = GFS2_I(inode); | |
558 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
559 | struct buffer_head *bh; | |
560 | struct gfs2_holder gh; | |
561 | int need_unlock = 0; | |
562 | int need_endtrans = 0; | |
563 | int ret; | |
564 | ||
0e11f644 | 565 | if (!(flags & I_DIRTY_INODE)) |
ab9bbda0 | 566 | return; |
eb43e660 | 567 | if (unlikely(gfs2_withdrawn(sdp))) |
0d1c7ae9 | 568 | return; |
ab9bbda0 SW |
569 | if (!gfs2_glock_is_locked_by_me(ip->i_gl)) { |
570 | ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); | |
571 | if (ret) { | |
572 | fs_err(sdp, "dirty_inode: glock %d\n", ret); | |
e28c02b9 | 573 | gfs2_dump_glock(NULL, ip->i_gl, true); |
ab9bbda0 SW |
574 | return; |
575 | } | |
576 | need_unlock = 1; | |
3d162688 BM |
577 | } else if (WARN_ON_ONCE(ip->i_gl->gl_state != LM_ST_EXCLUSIVE)) |
578 | return; | |
ab9bbda0 SW |
579 | |
580 | if (current->journal_info == NULL) { | |
581 | ret = gfs2_trans_begin(sdp, RES_DINODE, 0); | |
582 | if (ret) { | |
583 | fs_err(sdp, "dirty_inode: gfs2_trans_begin %d\n", ret); | |
584 | goto out; | |
585 | } | |
586 | need_endtrans = 1; | |
587 | } | |
588 | ||
589 | ret = gfs2_meta_inode_buffer(ip, &bh); | |
590 | if (ret == 0) { | |
350a9b0a | 591 | gfs2_trans_add_meta(ip->i_gl, bh); |
ab9bbda0 SW |
592 | gfs2_dinode_out(ip, bh->b_data); |
593 | brelse(bh); | |
594 | } | |
595 | ||
596 | if (need_endtrans) | |
597 | gfs2_trans_end(sdp); | |
598 | out: | |
599 | if (need_unlock) | |
600 | gfs2_glock_dq_uninit(&gh); | |
601 | } | |
602 | ||
9e6e0a12 SW |
603 | /** |
604 | * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one | |
605 | * @sdp: the filesystem | |
606 | * | |
607 | * Returns: errno | |
608 | */ | |
609 | ||
1f52aa08 | 610 | int gfs2_make_fs_ro(struct gfs2_sbd *sdp) |
9e6e0a12 | 611 | { |
2e60d768 | 612 | struct gfs2_holder freeze_gh; |
601ef0d5 BP |
613 | int error = 0; |
614 | int log_write_allowed = test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags); | |
615 | ||
616 | gfs2_holder_mark_uninitialized(&freeze_gh); | |
617 | if (sdp->sd_freeze_gl && | |
618 | !gfs2_glock_is_locked_by_me(sdp->sd_freeze_gl)) { | |
619 | if (!log_write_allowed) { | |
620 | error = gfs2_glock_nq_init(sdp->sd_freeze_gl, | |
c860f8ff BP |
621 | LM_ST_SHARED, LM_FLAG_TRY | |
622 | LM_FLAG_NOEXP | GL_EXACT, | |
623ba664 | 623 | &freeze_gh); |
601ef0d5 BP |
624 | if (error == GLR_TRYFAILED) |
625 | error = 0; | |
626 | } else { | |
627 | error = gfs2_glock_nq_init(sdp->sd_freeze_gl, | |
c860f8ff BP |
628 | LM_ST_SHARED, |
629 | LM_FLAG_NOEXP | GL_EXACT, | |
601ef0d5 BP |
630 | &freeze_gh); |
631 | if (error && !gfs2_withdrawn(sdp)) | |
632 | return error; | |
633 | } | |
634 | } | |
24972557 | 635 | |
a0e3cc65 | 636 | gfs2_flush_delete_work(sdp); |
601ef0d5 BP |
637 | if (!log_write_allowed && current == sdp->sd_quotad_process) |
638 | fs_warn(sdp, "The quotad daemon is withdrawing.\n"); | |
639 | else if (sdp->sd_quotad_process) | |
5b3a9f34 BP |
640 | kthread_stop(sdp->sd_quotad_process); |
641 | sdp->sd_quotad_process = NULL; | |
601ef0d5 BP |
642 | |
643 | if (!log_write_allowed && current == sdp->sd_logd_process) | |
644 | fs_warn(sdp, "The logd daemon is withdrawing.\n"); | |
645 | else if (sdp->sd_logd_process) | |
5b3a9f34 BP |
646 | kthread_stop(sdp->sd_logd_process); |
647 | sdp->sd_logd_process = NULL; | |
8ad151c2 | 648 | |
601ef0d5 BP |
649 | if (log_write_allowed) { |
650 | gfs2_quota_sync(sdp->sd_vfs, 0); | |
651 | gfs2_statfs_sync(sdp->sd_vfs, 0); | |
9e6e0a12 | 652 | |
601ef0d5 BP |
653 | gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_SHUTDOWN | |
654 | GFS2_LFC_MAKE_FS_RO); | |
655 | wait_event(sdp->sd_reserving_log_wait, | |
656 | atomic_read(&sdp->sd_reserving_log) == 0); | |
657 | gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == | |
658 | sdp->sd_jdesc->jd_blocks); | |
659 | } else { | |
660 | wait_event_timeout(sdp->sd_reserving_log_wait, | |
661 | atomic_read(&sdp->sd_reserving_log) == 0, | |
662 | HZ * 5); | |
663 | } | |
6df9f9a2 | 664 | if (gfs2_holder_initialized(&freeze_gh)) |
2e60d768 | 665 | gfs2_glock_dq_uninit(&freeze_gh); |
9e6e0a12 SW |
666 | |
667 | gfs2_quota_cleanup(sdp); | |
668 | ||
601ef0d5 BP |
669 | if (!log_write_allowed) |
670 | sdp->sd_vfs->s_flags |= SB_RDONLY; | |
671 | ||
9e6e0a12 SW |
672 | return error; |
673 | } | |
674 | ||
9e6e0a12 SW |
675 | /** |
676 | * gfs2_put_super - Unmount the filesystem | |
677 | * @sb: The VFS superblock | |
678 | * | |
679 | */ | |
680 | ||
681 | static void gfs2_put_super(struct super_block *sb) | |
682 | { | |
683 | struct gfs2_sbd *sdp = sb->s_fs_info; | |
684 | int error; | |
685 | struct gfs2_jdesc *jd; | |
686 | ||
9e6e0a12 SW |
687 | /* No more recovery requests */ |
688 | set_bit(SDF_NORECOVERY, &sdp->sd_flags); | |
689 | smp_mb(); | |
690 | ||
691 | /* Wait on outstanding recovery */ | |
692 | restart: | |
693 | spin_lock(&sdp->sd_jindex_spin); | |
694 | list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) { | |
695 | if (!test_bit(JDF_RECOVERY, &jd->jd_flags)) | |
696 | continue; | |
697 | spin_unlock(&sdp->sd_jindex_spin); | |
698 | wait_on_bit(&jd->jd_flags, JDF_RECOVERY, | |
74316201 | 699 | TASK_UNINTERRUPTIBLE); |
9e6e0a12 SW |
700 | goto restart; |
701 | } | |
702 | spin_unlock(&sdp->sd_jindex_spin); | |
703 | ||
bc98a42c | 704 | if (!sb_rdonly(sb)) { |
9e6e0a12 SW |
705 | error = gfs2_make_fs_ro(sdp); |
706 | if (error) | |
707 | gfs2_io_error(sdp); | |
708 | } | |
5a61ae14 AG |
709 | WARN_ON(gfs2_withdrawing(sdp)); |
710 | ||
9e6e0a12 SW |
711 | /* At this point, we're through modifying the disk */ |
712 | ||
713 | /* Release stuff */ | |
714 | ||
715 | iput(sdp->sd_jindex); | |
9e6e0a12 SW |
716 | iput(sdp->sd_statfs_inode); |
717 | iput(sdp->sd_rindex); | |
718 | iput(sdp->sd_quota_inode); | |
719 | ||
720 | gfs2_glock_put(sdp->sd_rename_gl); | |
24972557 | 721 | gfs2_glock_put(sdp->sd_freeze_gl); |
9e6e0a12 SW |
722 | |
723 | if (!sdp->sd_args.ar_spectator) { | |
601ef0d5 BP |
724 | if (gfs2_holder_initialized(&sdp->sd_journal_gh)) |
725 | gfs2_glock_dq_uninit(&sdp->sd_journal_gh); | |
726 | if (gfs2_holder_initialized(&sdp->sd_jinode_gh)) | |
727 | gfs2_glock_dq_uninit(&sdp->sd_jinode_gh); | |
9e6e0a12 SW |
728 | gfs2_glock_dq_uninit(&sdp->sd_sc_gh); |
729 | gfs2_glock_dq_uninit(&sdp->sd_qc_gh); | |
97fd734b | 730 | free_local_statfs_inodes(sdp); |
9e6e0a12 SW |
731 | iput(sdp->sd_qc_inode); |
732 | } | |
733 | ||
734 | gfs2_glock_dq_uninit(&sdp->sd_live_gh); | |
735 | gfs2_clear_rgrpd(sdp); | |
736 | gfs2_jindex_free(sdp); | |
737 | /* Take apart glock structures and buffer lists */ | |
738 | gfs2_gl_hash_clear(sdp); | |
a9dd945c | 739 | truncate_inode_pages_final(&sdp->sd_aspace); |
b2fb7dab | 740 | gfs2_delete_debugfs_file(sdp); |
9e6e0a12 SW |
741 | /* Unmount the locking protocol */ |
742 | gfs2_lm_unmount(sdp); | |
743 | ||
744 | /* At this point, we're through participating in the lockspace */ | |
745 | gfs2_sys_fs_del(sdp); | |
c2a04b02 | 746 | free_sbd(sdp); |
9e6e0a12 SW |
747 | } |
748 | ||
9e6e0a12 SW |
749 | /** |
750 | * gfs2_sync_fs - sync the filesystem | |
751 | * @sb: the superblock | |
752 | * | |
753 | * Flushes the log to disk. | |
754 | */ | |
755 | ||
756 | static int gfs2_sync_fs(struct super_block *sb, int wait) | |
757 | { | |
1027efaa | 758 | struct gfs2_sbd *sdp = sb->s_fs_info; |
a1177825 JK |
759 | |
760 | gfs2_quota_sync(sb, -1); | |
942b0cdd | 761 | if (wait) |
805c0907 BP |
762 | gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL | |
763 | GFS2_LFC_SYNC_FS); | |
942b0cdd | 764 | return sdp->sd_log_error; |
9e6e0a12 SW |
765 | } |
766 | ||
2e60d768 BM |
767 | void gfs2_freeze_func(struct work_struct *work) |
768 | { | |
769 | int error; | |
770 | struct gfs2_holder freeze_gh; | |
771 | struct gfs2_sbd *sdp = container_of(work, struct gfs2_sbd, sd_freeze_work); | |
772 | struct super_block *sb = sdp->sd_vfs; | |
773 | ||
774 | atomic_inc(&sb->s_active); | |
c860f8ff BP |
775 | error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, |
776 | LM_FLAG_NOEXP | GL_EXACT, &freeze_gh); | |
2e60d768 | 777 | if (error) { |
f29e62ee | 778 | fs_info(sdp, "GFS2: couldn't get freeze lock : %d\n", error); |
2e60d768 | 779 | gfs2_assert_withdraw(sdp, 0); |
8f918219 | 780 | } else { |
2e60d768 BM |
781 | atomic_set(&sdp->sd_freeze_state, SFS_UNFROZEN); |
782 | error = thaw_super(sb); | |
783 | if (error) { | |
f29e62ee BP |
784 | fs_info(sdp, "GFS2: couldn't thaw filesystem: %d\n", |
785 | error); | |
2e60d768 BM |
786 | gfs2_assert_withdraw(sdp, 0); |
787 | } | |
2e60d768 BM |
788 | gfs2_glock_dq_uninit(&freeze_gh); |
789 | } | |
790 | deactivate_super(sb); | |
8f918219 AD |
791 | clear_bit_unlock(SDF_FS_FROZEN, &sdp->sd_flags); |
792 | wake_up_bit(&sdp->sd_flags, SDF_FS_FROZEN); | |
2e60d768 BM |
793 | return; |
794 | } | |
795 | ||
9e6e0a12 SW |
796 | /** |
797 | * gfs2_freeze - prevent further writes to the filesystem | |
798 | * @sb: the VFS structure for the filesystem | |
799 | * | |
800 | */ | |
801 | ||
802 | static int gfs2_freeze(struct super_block *sb) | |
803 | { | |
804 | struct gfs2_sbd *sdp = sb->s_fs_info; | |
2e60d768 | 805 | int error = 0; |
9e6e0a12 | 806 | |
2e60d768 BM |
807 | mutex_lock(&sdp->sd_freeze_mutex); |
808 | if (atomic_read(&sdp->sd_freeze_state) != SFS_UNFROZEN) | |
809 | goto out; | |
810 | ||
9e6e0a12 | 811 | for (;;) { |
60528afa BP |
812 | if (gfs2_withdrawn(sdp)) { |
813 | error = -EINVAL; | |
814 | goto out; | |
815 | } | |
816 | ||
52b1cdcb | 817 | error = gfs2_lock_fs_check_clean(sdp); |
9e6e0a12 SW |
818 | if (!error) |
819 | break; | |
820 | ||
55317f5b | 821 | if (error == -EBUSY) |
9e6e0a12 | 822 | fs_err(sdp, "waiting for recovery before freeze\n"); |
52b1cdcb BP |
823 | else if (error == -EIO) { |
824 | fs_err(sdp, "Fatal IO error: cannot freeze gfs2 due " | |
825 | "to recovery error.\n"); | |
826 | goto out; | |
827 | } else { | |
9e6e0a12 | 828 | fs_err(sdp, "error freezing FS: %d\n", error); |
52b1cdcb | 829 | } |
9e6e0a12 SW |
830 | fs_err(sdp, "retrying...\n"); |
831 | msleep(1000); | |
832 | } | |
8f918219 | 833 | set_bit(SDF_FS_FROZEN, &sdp->sd_flags); |
2e60d768 BM |
834 | out: |
835 | mutex_unlock(&sdp->sd_freeze_mutex); | |
836 | return error; | |
9e6e0a12 SW |
837 | } |
838 | ||
839 | /** | |
840 | * gfs2_unfreeze - reallow writes to the filesystem | |
841 | * @sb: the VFS structure for the filesystem | |
842 | * | |
843 | */ | |
844 | ||
845 | static int gfs2_unfreeze(struct super_block *sb) | |
846 | { | |
d564053f SW |
847 | struct gfs2_sbd *sdp = sb->s_fs_info; |
848 | ||
2e60d768 BM |
849 | mutex_lock(&sdp->sd_freeze_mutex); |
850 | if (atomic_read(&sdp->sd_freeze_state) != SFS_FROZEN || | |
6df9f9a2 | 851 | !gfs2_holder_initialized(&sdp->sd_freeze_gh)) { |
2e60d768 BM |
852 | mutex_unlock(&sdp->sd_freeze_mutex); |
853 | return 0; | |
854 | } | |
855 | ||
d564053f | 856 | gfs2_glock_dq_uninit(&sdp->sd_freeze_gh); |
2e60d768 | 857 | mutex_unlock(&sdp->sd_freeze_mutex); |
8f918219 | 858 | return wait_on_bit(&sdp->sd_flags, SDF_FS_FROZEN, TASK_INTERRUPTIBLE); |
9e6e0a12 SW |
859 | } |
860 | ||
861 | /** | |
862 | * statfs_fill - fill in the sg for a given RG | |
863 | * @rgd: the RG | |
864 | * @sc: the sc structure | |
865 | * | |
866 | * Returns: 0 on success, -ESTALE if the LVB is invalid | |
867 | */ | |
868 | ||
869 | static int statfs_slow_fill(struct gfs2_rgrpd *rgd, | |
870 | struct gfs2_statfs_change_host *sc) | |
871 | { | |
872 | gfs2_rgrp_verify(rgd); | |
873 | sc->sc_total += rgd->rd_data; | |
874 | sc->sc_free += rgd->rd_free; | |
875 | sc->sc_dinodes += rgd->rd_dinodes; | |
876 | return 0; | |
877 | } | |
878 | ||
879 | /** | |
880 | * gfs2_statfs_slow - Stat a filesystem using asynchronous locking | |
881 | * @sdp: the filesystem | |
882 | * @sc: the sc info that will be returned | |
883 | * | |
884 | * Any error (other than a signal) will cause this routine to fall back | |
885 | * to the synchronous version. | |
886 | * | |
887 | * FIXME: This really shouldn't busy wait like this. | |
888 | * | |
889 | * Returns: errno | |
890 | */ | |
891 | ||
892 | static int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc) | |
893 | { | |
9e6e0a12 SW |
894 | struct gfs2_rgrpd *rgd_next; |
895 | struct gfs2_holder *gha, *gh; | |
896 | unsigned int slots = 64; | |
897 | unsigned int x; | |
898 | int done; | |
899 | int error = 0, err; | |
900 | ||
901 | memset(sc, 0, sizeof(struct gfs2_statfs_change_host)); | |
6da2ec56 | 902 | gha = kmalloc_array(slots, sizeof(struct gfs2_holder), GFP_KERNEL); |
9e6e0a12 SW |
903 | if (!gha) |
904 | return -ENOMEM; | |
6df9f9a2 AG |
905 | for (x = 0; x < slots; x++) |
906 | gfs2_holder_mark_uninitialized(gha + x); | |
9e6e0a12 | 907 | |
9e6e0a12 SW |
908 | rgd_next = gfs2_rgrpd_get_first(sdp); |
909 | ||
910 | for (;;) { | |
911 | done = 1; | |
912 | ||
913 | for (x = 0; x < slots; x++) { | |
914 | gh = gha + x; | |
915 | ||
6df9f9a2 | 916 | if (gfs2_holder_initialized(gh) && gfs2_glock_poll(gh)) { |
9e6e0a12 SW |
917 | err = gfs2_glock_wait(gh); |
918 | if (err) { | |
919 | gfs2_holder_uninit(gh); | |
920 | error = err; | |
921 | } else { | |
6f6597ba AG |
922 | if (!error) { |
923 | struct gfs2_rgrpd *rgd = | |
924 | gfs2_glock2rgrp(gh->gh_gl); | |
925 | ||
926 | error = statfs_slow_fill(rgd, sc); | |
927 | } | |
9e6e0a12 SW |
928 | gfs2_glock_dq_uninit(gh); |
929 | } | |
930 | } | |
931 | ||
6df9f9a2 | 932 | if (gfs2_holder_initialized(gh)) |
9e6e0a12 SW |
933 | done = 0; |
934 | else if (rgd_next && !error) { | |
935 | error = gfs2_glock_nq_init(rgd_next->rd_gl, | |
936 | LM_ST_SHARED, | |
937 | GL_ASYNC, | |
938 | gh); | |
939 | rgd_next = gfs2_rgrpd_get_next(rgd_next); | |
940 | done = 0; | |
941 | } | |
942 | ||
943 | if (signal_pending(current)) | |
944 | error = -ERESTARTSYS; | |
945 | } | |
946 | ||
947 | if (done) | |
948 | break; | |
949 | ||
950 | yield(); | |
951 | } | |
952 | ||
9e6e0a12 SW |
953 | kfree(gha); |
954 | return error; | |
955 | } | |
956 | ||
957 | /** | |
958 | * gfs2_statfs_i - Do a statfs | |
959 | * @sdp: the filesystem | |
960 | * @sg: the sg structure | |
961 | * | |
962 | * Returns: errno | |
963 | */ | |
964 | ||
965 | static int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc) | |
966 | { | |
967 | struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master; | |
968 | struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local; | |
969 | ||
970 | spin_lock(&sdp->sd_statfs_spin); | |
971 | ||
972 | *sc = *m_sc; | |
973 | sc->sc_total += l_sc->sc_total; | |
974 | sc->sc_free += l_sc->sc_free; | |
975 | sc->sc_dinodes += l_sc->sc_dinodes; | |
976 | ||
977 | spin_unlock(&sdp->sd_statfs_spin); | |
978 | ||
979 | if (sc->sc_free < 0) | |
980 | sc->sc_free = 0; | |
981 | if (sc->sc_free > sc->sc_total) | |
982 | sc->sc_free = sc->sc_total; | |
983 | if (sc->sc_dinodes < 0) | |
984 | sc->sc_dinodes = 0; | |
985 | ||
986 | return 0; | |
987 | } | |
988 | ||
989 | /** | |
990 | * gfs2_statfs - Gather and return stats about the filesystem | |
991 | * @sb: The superblock | |
992 | * @statfsbuf: The buffer | |
993 | * | |
994 | * Returns: 0 on success or error code | |
995 | */ | |
996 | ||
997 | static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf) | |
998 | { | |
fc64005c | 999 | struct super_block *sb = dentry->d_sb; |
9e6e0a12 SW |
1000 | struct gfs2_sbd *sdp = sb->s_fs_info; |
1001 | struct gfs2_statfs_change_host sc; | |
1002 | int error; | |
1003 | ||
8339ee54 SW |
1004 | error = gfs2_rindex_update(sdp); |
1005 | if (error) | |
1006 | return error; | |
1007 | ||
9e6e0a12 SW |
1008 | if (gfs2_tune_get(sdp, gt_statfs_slow)) |
1009 | error = gfs2_statfs_slow(sdp, &sc); | |
1010 | else | |
1011 | error = gfs2_statfs_i(sdp, &sc); | |
1012 | ||
1013 | if (error) | |
1014 | return error; | |
1015 | ||
1016 | buf->f_type = GFS2_MAGIC; | |
1017 | buf->f_bsize = sdp->sd_sb.sb_bsize; | |
1018 | buf->f_blocks = sc.sc_total; | |
1019 | buf->f_bfree = sc.sc_free; | |
1020 | buf->f_bavail = sc.sc_free; | |
1021 | buf->f_files = sc.sc_dinodes + sc.sc_free; | |
1022 | buf->f_ffree = sc.sc_free; | |
1023 | buf->f_namelen = GFS2_FNAMESIZE; | |
1024 | ||
1025 | return 0; | |
1026 | } | |
1027 | ||
9e6e0a12 SW |
1028 | /** |
1029 | * gfs2_drop_inode - Drop an inode (test for remote unlink) | |
1030 | * @inode: The inode to drop | |
1031 | * | |
61b91cfd | 1032 | * If we've received a callback on an iopen lock then it's because a |
9e6e0a12 SW |
1033 | * remote node tried to deallocate the inode but failed due to this node |
1034 | * still having the inode open. Here we mark the link count zero | |
1035 | * since we know that it must have reached zero if the GLF_DEMOTE flag | |
1036 | * is set on the iopen glock. If we didn't do a disk read since the | |
1037 | * remote node removed the final link then we might otherwise miss | |
1038 | * this event. This check ensures that this node will deallocate the | |
1039 | * inode's blocks, or alternatively pass the baton on to another | |
1040 | * node for later deallocation. | |
1041 | */ | |
1042 | ||
45321ac5 | 1043 | static int gfs2_drop_inode(struct inode *inode) |
9e6e0a12 SW |
1044 | { |
1045 | struct gfs2_inode *ip = GFS2_I(inode); | |
1046 | ||
6df9f9a2 AG |
1047 | if (!test_bit(GIF_FREE_VFS_INODE, &ip->i_flags) && |
1048 | inode->i_nlink && | |
1049 | gfs2_holder_initialized(&ip->i_iopen_gh)) { | |
9e6e0a12 | 1050 | struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl; |
6df9f9a2 | 1051 | if (test_bit(GLF_DEMOTE, &gl->gl_flags)) |
9e6e0a12 SW |
1052 | clear_nlink(inode); |
1053 | } | |
6a1c8f6d AG |
1054 | |
1055 | /* | |
1056 | * When under memory pressure when an inode's link count has dropped to | |
1057 | * zero, defer deleting the inode to the delete workqueue. This avoids | |
1058 | * calling into DLM under memory pressure, which can deadlock. | |
1059 | */ | |
1060 | if (!inode->i_nlink && | |
1061 | unlikely(current->flags & PF_MEMALLOC) && | |
1062 | gfs2_holder_initialized(&ip->i_iopen_gh)) { | |
1063 | struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl; | |
1064 | ||
1065 | gfs2_glock_hold(gl); | |
a0e3cc65 | 1066 | if (!gfs2_queue_delete_work(gl, 0)) |
6a1c8f6d AG |
1067 | gfs2_glock_queue_put(gl); |
1068 | return false; | |
1069 | } | |
1070 | ||
45321ac5 | 1071 | return generic_drop_inode(inode); |
9e6e0a12 SW |
1072 | } |
1073 | ||
1074 | static int is_ancestor(const struct dentry *d1, const struct dentry *d2) | |
1075 | { | |
1076 | do { | |
1077 | if (d1 == d2) | |
1078 | return 1; | |
1079 | d1 = d1->d_parent; | |
1080 | } while (!IS_ROOT(d1)); | |
1081 | return 0; | |
1082 | } | |
1083 | ||
1084 | /** | |
1085 | * gfs2_show_options - Show mount options for /proc/mounts | |
1086 | * @s: seq_file structure | |
34c80b1d | 1087 | * @root: root of this (sub)tree |
9e6e0a12 SW |
1088 | * |
1089 | * Returns: 0 on success or error code | |
1090 | */ | |
1091 | ||
34c80b1d | 1092 | static int gfs2_show_options(struct seq_file *s, struct dentry *root) |
9e6e0a12 | 1093 | { |
34c80b1d | 1094 | struct gfs2_sbd *sdp = root->d_sb->s_fs_info; |
9e6e0a12 | 1095 | struct gfs2_args *args = &sdp->sd_args; |
3d3c10f2 | 1096 | int val; |
9e6e0a12 | 1097 | |
34c80b1d | 1098 | if (is_ancestor(root, sdp->sd_master_dir)) |
eaebdedc | 1099 | seq_puts(s, ",meta"); |
9e6e0a12 | 1100 | if (args->ar_lockproto[0]) |
a068acf2 | 1101 | seq_show_option(s, "lockproto", args->ar_lockproto); |
9e6e0a12 | 1102 | if (args->ar_locktable[0]) |
a068acf2 | 1103 | seq_show_option(s, "locktable", args->ar_locktable); |
9e6e0a12 | 1104 | if (args->ar_hostdata[0]) |
a068acf2 | 1105 | seq_show_option(s, "hostdata", args->ar_hostdata); |
9e6e0a12 | 1106 | if (args->ar_spectator) |
eaebdedc | 1107 | seq_puts(s, ",spectator"); |
9e6e0a12 | 1108 | if (args->ar_localflocks) |
eaebdedc | 1109 | seq_puts(s, ",localflocks"); |
9e6e0a12 | 1110 | if (args->ar_debug) |
eaebdedc | 1111 | seq_puts(s, ",debug"); |
9e6e0a12 | 1112 | if (args->ar_posix_acl) |
eaebdedc | 1113 | seq_puts(s, ",acl"); |
9e6e0a12 SW |
1114 | if (args->ar_quota != GFS2_QUOTA_DEFAULT) { |
1115 | char *state; | |
1116 | switch (args->ar_quota) { | |
1117 | case GFS2_QUOTA_OFF: | |
1118 | state = "off"; | |
1119 | break; | |
1120 | case GFS2_QUOTA_ACCOUNT: | |
1121 | state = "account"; | |
1122 | break; | |
1123 | case GFS2_QUOTA_ON: | |
1124 | state = "on"; | |
1125 | break; | |
1126 | default: | |
1127 | state = "unknown"; | |
1128 | break; | |
1129 | } | |
1130 | seq_printf(s, ",quota=%s", state); | |
1131 | } | |
1132 | if (args->ar_suiddir) | |
eaebdedc | 1133 | seq_puts(s, ",suiddir"); |
9e6e0a12 SW |
1134 | if (args->ar_data != GFS2_DATA_DEFAULT) { |
1135 | char *state; | |
1136 | switch (args->ar_data) { | |
1137 | case GFS2_DATA_WRITEBACK: | |
1138 | state = "writeback"; | |
1139 | break; | |
1140 | case GFS2_DATA_ORDERED: | |
1141 | state = "ordered"; | |
1142 | break; | |
1143 | default: | |
1144 | state = "unknown"; | |
1145 | break; | |
1146 | } | |
1147 | seq_printf(s, ",data=%s", state); | |
1148 | } | |
1149 | if (args->ar_discard) | |
eaebdedc | 1150 | seq_puts(s, ",discard"); |
5e687eac BM |
1151 | val = sdp->sd_tune.gt_logd_secs; |
1152 | if (val != 30) | |
3d3c10f2 BM |
1153 | seq_printf(s, ",commit=%d", val); |
1154 | val = sdp->sd_tune.gt_statfs_quantum; | |
1155 | if (val != 30) | |
1156 | seq_printf(s, ",statfs_quantum=%d", val); | |
2b9731e8 SW |
1157 | else if (sdp->sd_tune.gt_statfs_slow) |
1158 | seq_puts(s, ",statfs_quantum=0"); | |
3d3c10f2 BM |
1159 | val = sdp->sd_tune.gt_quota_quantum; |
1160 | if (val != 60) | |
1161 | seq_printf(s, ",quota_quantum=%d", val); | |
1162 | if (args->ar_statfs_percent) | |
1163 | seq_printf(s, ",statfs_percent=%d", args->ar_statfs_percent); | |
d34843d0 BP |
1164 | if (args->ar_errors != GFS2_ERRORS_DEFAULT) { |
1165 | const char *state; | |
1166 | ||
1167 | switch (args->ar_errors) { | |
1168 | case GFS2_ERRORS_WITHDRAW: | |
1169 | state = "withdraw"; | |
1170 | break; | |
1171 | case GFS2_ERRORS_PANIC: | |
1172 | state = "panic"; | |
1173 | break; | |
1174 | default: | |
1175 | state = "unknown"; | |
1176 | break; | |
1177 | } | |
1178 | seq_printf(s, ",errors=%s", state); | |
1179 | } | |
cdcfde62 | 1180 | if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) |
eaebdedc | 1181 | seq_puts(s, ",nobarrier"); |
913a71d2 | 1182 | if (test_bit(SDF_DEMOTE, &sdp->sd_flags)) |
eaebdedc | 1183 | seq_puts(s, ",demote_interface_used"); |
90306c41 | 1184 | if (args->ar_rgrplvb) |
eaebdedc | 1185 | seq_puts(s, ",rgrplvb"); |
471f3db2 BM |
1186 | if (args->ar_loccookie) |
1187 | seq_puts(s, ",loccookie"); | |
9e6e0a12 SW |
1188 | return 0; |
1189 | } | |
1190 | ||
f42ab085 SW |
1191 | static void gfs2_final_release_pages(struct gfs2_inode *ip) |
1192 | { | |
1193 | struct inode *inode = &ip->i_inode; | |
1194 | struct gfs2_glock *gl = ip->i_gl; | |
1195 | ||
1196 | truncate_inode_pages(gfs2_glock2aspace(ip->i_gl), 0); | |
1197 | truncate_inode_pages(&inode->i_data, 0); | |
1198 | ||
638803d4 | 1199 | if (atomic_read(&gl->gl_revokes) == 0) { |
f42ab085 SW |
1200 | clear_bit(GLF_LFLUSH, &gl->gl_flags); |
1201 | clear_bit(GLF_DIRTY, &gl->gl_flags); | |
1202 | } | |
1203 | } | |
1204 | ||
1205 | static int gfs2_dinode_dealloc(struct gfs2_inode *ip) | |
1206 | { | |
1207 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); | |
f42ab085 | 1208 | struct gfs2_rgrpd *rgd; |
564e12b1 | 1209 | struct gfs2_holder gh; |
f42ab085 SW |
1210 | int error; |
1211 | ||
1212 | if (gfs2_get_inode_blocks(&ip->i_inode) != 1) { | |
94fb763b | 1213 | gfs2_consist_inode(ip); |
f42ab085 SW |
1214 | return -EIO; |
1215 | } | |
1216 | ||
8e2e0047 BP |
1217 | error = gfs2_rindex_update(sdp); |
1218 | if (error) | |
1219 | return error; | |
f42ab085 | 1220 | |
f4108a60 | 1221 | error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE); |
f42ab085 | 1222 | if (error) |
5407e242 | 1223 | return error; |
f42ab085 | 1224 | |
66fc061b | 1225 | rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1); |
f42ab085 SW |
1226 | if (!rgd) { |
1227 | gfs2_consist_inode(ip); | |
1228 | error = -EIO; | |
8339ee54 | 1229 | goto out_qs; |
f42ab085 SW |
1230 | } |
1231 | ||
564e12b1 | 1232 | error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh); |
f42ab085 | 1233 | if (error) |
8339ee54 | 1234 | goto out_qs; |
f42ab085 | 1235 | |
4667a0ec SW |
1236 | error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, |
1237 | sdp->sd_jdesc->jd_blocks); | |
f42ab085 SW |
1238 | if (error) |
1239 | goto out_rg_gunlock; | |
1240 | ||
1241 | gfs2_free_di(rgd, ip); | |
1242 | ||
1243 | gfs2_final_release_pages(ip); | |
1244 | ||
1245 | gfs2_trans_end(sdp); | |
1246 | ||
1247 | out_rg_gunlock: | |
564e12b1 | 1248 | gfs2_glock_dq_uninit(&gh); |
f42ab085 SW |
1249 | out_qs: |
1250 | gfs2_quota_unhold(ip); | |
f42ab085 SW |
1251 | return error; |
1252 | } | |
1253 | ||
71c1b213 AG |
1254 | /** |
1255 | * gfs2_glock_put_eventually | |
1256 | * @gl: The glock to put | |
1257 | * | |
1258 | * When under memory pressure, trigger a deferred glock put to make sure we | |
1259 | * won't call into DLM and deadlock. Otherwise, put the glock directly. | |
1260 | */ | |
1261 | ||
1262 | static void gfs2_glock_put_eventually(struct gfs2_glock *gl) | |
1263 | { | |
1264 | if (current->flags & PF_MEMALLOC) | |
1265 | gfs2_glock_queue_put(gl); | |
1266 | else | |
1267 | gfs2_glock_put(gl); | |
1268 | } | |
1269 | ||
9e73330f AG |
1270 | static bool gfs2_upgrade_iopen_glock(struct inode *inode) |
1271 | { | |
1272 | struct gfs2_inode *ip = GFS2_I(inode); | |
1273 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
1274 | struct gfs2_holder *gh = &ip->i_iopen_gh; | |
1275 | long timeout = 5 * HZ; | |
1276 | int error; | |
1277 | ||
1278 | gh->gh_flags |= GL_NOCACHE; | |
1279 | gfs2_glock_dq_wait(gh); | |
1280 | ||
1281 | /* | |
1282 | * If there are no other lock holders, we'll get the lock immediately. | |
1283 | * Otherwise, the other nodes holding the lock will be notified about | |
1284 | * our locking request. If they don't have the inode open, they'll | |
9e8990de AG |
1285 | * evict the cached inode and release the lock. Otherwise, if they |
1286 | * poke the inode glock, we'll take this as an indication that they | |
1287 | * still need the iopen glock and that they'll take care of deleting | |
1288 | * the inode when they're done. As a last resort, if another node | |
1289 | * keeps holding the iopen glock without showing any activity on the | |
1290 | * inode glock, we'll eventually time out. | |
9e73330f AG |
1291 | * |
1292 | * Note that we're passing the LM_FLAG_TRY_1CB flag to the first | |
1293 | * locking request as an optimization to notify lock holders as soon as | |
1294 | * possible. Without that flag, they'd be notified implicitly by the | |
1295 | * second locking request. | |
1296 | */ | |
1297 | ||
1298 | gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, gh); | |
1299 | error = gfs2_glock_nq(gh); | |
1300 | if (error != GLR_TRYFAILED) | |
1301 | return !error; | |
1302 | ||
1303 | gfs2_holder_reinit(LM_ST_EXCLUSIVE, GL_ASYNC | GL_NOCACHE, gh); | |
1304 | error = gfs2_glock_nq(gh); | |
1305 | if (error) | |
1306 | return false; | |
1307 | ||
1308 | timeout = wait_event_interruptible_timeout(sdp->sd_async_glock_wait, | |
9e8990de AG |
1309 | !test_bit(HIF_WAIT, &gh->gh_iflags) || |
1310 | test_bit(GLF_DEMOTE, &ip->i_gl->gl_flags), | |
9e73330f AG |
1311 | timeout); |
1312 | if (!test_bit(HIF_HOLDER, &gh->gh_iflags)) { | |
1313 | gfs2_glock_dq(gh); | |
1314 | return false; | |
1315 | } | |
1316 | return true; | |
1317 | } | |
1318 | ||
53dbc27e BP |
1319 | /** |
1320 | * evict_should_delete - determine whether the inode is eligible for deletion | |
1321 | * @inode: The inode to evict | |
1322 | * | |
1323 | * This function determines whether the evicted inode is eligible to be deleted | |
1324 | * and locks the inode glock. | |
1325 | * | |
1326 | * Returns: the fate of the dinode | |
1327 | */ | |
1328 | static enum dinode_demise evict_should_delete(struct inode *inode, | |
1329 | struct gfs2_holder *gh) | |
1330 | { | |
1331 | struct gfs2_inode *ip = GFS2_I(inode); | |
1332 | struct super_block *sb = inode->i_sb; | |
1333 | struct gfs2_sbd *sdp = sb->s_fs_info; | |
1334 | int ret; | |
1335 | ||
1336 | if (test_bit(GIF_ALLOC_FAILED, &ip->i_flags)) { | |
1337 | BUG_ON(!gfs2_glock_is_locked_by_me(ip->i_gl)); | |
1338 | goto should_delete; | |
1339 | } | |
1340 | ||
1341 | if (test_bit(GIF_DEFERRED_DELETE, &ip->i_flags)) | |
1342 | return SHOULD_DEFER_EVICTION; | |
1343 | ||
1344 | /* Deletes should never happen under memory pressure anymore. */ | |
1345 | if (WARN_ON_ONCE(current->flags & PF_MEMALLOC)) | |
1346 | return SHOULD_DEFER_EVICTION; | |
1347 | ||
1348 | /* Must not read inode block until block type has been verified */ | |
1349 | ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, gh); | |
1350 | if (unlikely(ret)) { | |
1351 | glock_clear_object(ip->i_iopen_gh.gh_gl, ip); | |
1352 | ip->i_iopen_gh.gh_flags |= GL_NOCACHE; | |
1353 | gfs2_glock_dq_uninit(&ip->i_iopen_gh); | |
1354 | return SHOULD_DEFER_EVICTION; | |
1355 | } | |
1356 | ||
1357 | if (gfs2_inode_already_deleted(ip->i_gl, ip->i_no_formal_ino)) | |
1358 | return SHOULD_NOT_DELETE_DINODE; | |
1359 | ret = gfs2_check_blk_type(sdp, ip->i_no_addr, GFS2_BLKST_UNLINKED); | |
1360 | if (ret) | |
1361 | return SHOULD_NOT_DELETE_DINODE; | |
1362 | ||
1363 | if (test_bit(GIF_INVALID, &ip->i_flags)) { | |
1364 | ret = gfs2_inode_refresh(ip); | |
1365 | if (ret) | |
1366 | return SHOULD_NOT_DELETE_DINODE; | |
1367 | } | |
1368 | ||
1369 | /* | |
1370 | * The inode may have been recreated in the meantime. | |
1371 | */ | |
1372 | if (inode->i_nlink) | |
1373 | return SHOULD_NOT_DELETE_DINODE; | |
1374 | ||
1375 | should_delete: | |
1376 | if (gfs2_holder_initialized(&ip->i_iopen_gh) && | |
1377 | test_bit(HIF_HOLDER, &ip->i_iopen_gh.gh_iflags)) { | |
1378 | if (!gfs2_upgrade_iopen_glock(inode)) { | |
1379 | gfs2_holder_uninit(&ip->i_iopen_gh); | |
1380 | return SHOULD_NOT_DELETE_DINODE; | |
1381 | } | |
1382 | } | |
1383 | return SHOULD_DELETE_DINODE; | |
1384 | } | |
1385 | ||
6e7e9a50 BP |
1386 | /** |
1387 | * evict_unlinked_inode - delete the pieces of an unlinked evicted inode | |
1388 | * @inode: The inode to evict | |
1389 | */ | |
1390 | static int evict_unlinked_inode(struct inode *inode) | |
1391 | { | |
1392 | struct gfs2_inode *ip = GFS2_I(inode); | |
1393 | int ret; | |
1394 | ||
1395 | if (S_ISDIR(inode->i_mode) && | |
1396 | (ip->i_diskflags & GFS2_DIF_EXHASH)) { | |
1397 | ret = gfs2_dir_exhash_dealloc(ip); | |
1398 | if (ret) | |
1399 | goto out; | |
1400 | } | |
1401 | ||
1402 | if (ip->i_eattr) { | |
1403 | ret = gfs2_ea_dealloc(ip); | |
1404 | if (ret) | |
1405 | goto out; | |
1406 | } | |
1407 | ||
1408 | if (!gfs2_is_stuffed(ip)) { | |
1409 | ret = gfs2_file_dealloc(ip); | |
1410 | if (ret) | |
1411 | goto out; | |
1412 | } | |
1413 | ||
1414 | /* We're about to clear the bitmap for the dinode, but as soon as we | |
1415 | do, gfs2_create_inode can create another inode at the same block | |
1416 | location and try to set gl_object again. We clear gl_object here so | |
1417 | that subsequent inode creates don't see an old gl_object. */ | |
1418 | glock_clear_object(ip->i_gl, ip); | |
1419 | ret = gfs2_dinode_dealloc(ip); | |
1420 | gfs2_inode_remember_delete(ip->i_gl, ip->i_no_formal_ino); | |
1421 | out: | |
1422 | return ret; | |
1423 | } | |
1424 | ||
d90be6ab BP |
1425 | /* |
1426 | * evict_linked_inode - evict an inode whose dinode has not been unlinked | |
1427 | * @inode: The inode to evict | |
1428 | */ | |
1429 | static int evict_linked_inode(struct inode *inode) | |
1430 | { | |
1431 | struct super_block *sb = inode->i_sb; | |
1432 | struct gfs2_sbd *sdp = sb->s_fs_info; | |
1433 | struct gfs2_inode *ip = GFS2_I(inode); | |
1434 | struct address_space *metamapping; | |
1435 | int ret; | |
1436 | ||
1437 | gfs2_log_flush(sdp, ip->i_gl, GFS2_LOG_HEAD_FLUSH_NORMAL | | |
1438 | GFS2_LFC_EVICT_INODE); | |
1439 | metamapping = gfs2_glock2aspace(ip->i_gl); | |
1440 | if (test_bit(GLF_DIRTY, &ip->i_gl->gl_flags)) { | |
1441 | filemap_fdatawrite(metamapping); | |
1442 | filemap_fdatawait(metamapping); | |
1443 | } | |
1444 | write_inode_now(inode, 1); | |
1445 | gfs2_ail_flush(ip->i_gl, 0); | |
1446 | ||
1447 | ret = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks); | |
1448 | if (ret) | |
1449 | return ret; | |
1450 | ||
1451 | /* Needs to be done before glock release & also in a transaction */ | |
1452 | truncate_inode_pages(&inode->i_data, 0); | |
1453 | truncate_inode_pages(metamapping, 0); | |
1454 | gfs2_trans_end(sdp); | |
1455 | return 0; | |
1456 | } | |
1457 | ||
380f7c65 SW |
1458 | /** |
1459 | * gfs2_evict_inode - Remove an inode from cache | |
1460 | * @inode: The inode to evict | |
1461 | * | |
1462 | * There are three cases to consider: | |
1463 | * 1. i_nlink == 0, we are final opener (and must deallocate) | |
1464 | * 2. i_nlink == 0, we are not the final opener (and cannot deallocate) | |
1465 | * 3. i_nlink > 0 | |
1466 | * | |
1467 | * If the fs is read only, then we have to treat all cases as per #3 | |
1468 | * since we are unable to do any deallocation. The inode will be | |
1469 | * deallocated by the next read/write node to attempt an allocation | |
1470 | * in the same resource group | |
1471 | * | |
9e6e0a12 SW |
1472 | * We have to (at the moment) hold the inodes main lock to cover |
1473 | * the gap between unlocking the shared lock on the iopen lock and | |
1474 | * taking the exclusive lock. I'd rather do a shared -> exclusive | |
1475 | * conversion on the iopen lock, but we can change that later. This | |
1476 | * is safe, just less efficient. | |
1477 | */ | |
1478 | ||
d5c1515c | 1479 | static void gfs2_evict_inode(struct inode *inode) |
9e6e0a12 | 1480 | { |
001e8e8d SW |
1481 | struct super_block *sb = inode->i_sb; |
1482 | struct gfs2_sbd *sdp = sb->s_fs_info; | |
9e6e0a12 SW |
1483 | struct gfs2_inode *ip = GFS2_I(inode); |
1484 | struct gfs2_holder gh; | |
23d828fc | 1485 | int ret; |
9e6e0a12 | 1486 | |
05978803 AD |
1487 | if (test_bit(GIF_FREE_VFS_INODE, &ip->i_flags)) { |
1488 | clear_inode(inode); | |
1489 | return; | |
1490 | } | |
1491 | ||
bc98a42c | 1492 | if (inode->i_nlink || sb_rdonly(sb)) |
d5c1515c AV |
1493 | goto out; |
1494 | ||
53dbc27e BP |
1495 | gfs2_holder_mark_uninitialized(&gh); |
1496 | ret = evict_should_delete(inode, &gh); | |
1497 | if (ret == SHOULD_DEFER_EVICTION) | |
8c7b9262 | 1498 | goto out; |
0a0d9f55 BP |
1499 | if (ret == SHOULD_DELETE_DINODE) |
1500 | ret = evict_unlinked_inode(inode); | |
1501 | else | |
1502 | ret = evict_linked_inode(inode); | |
9e6e0a12 | 1503 | |
a097dc7e BP |
1504 | if (gfs2_rs_active(&ip->i_res)) |
1505 | gfs2_rs_deltree(&ip->i_res); | |
8e2e0047 | 1506 | |
240c6235 BP |
1507 | if (gfs2_holder_initialized(&gh)) { |
1508 | glock_clear_object(ip->i_gl, ip); | |
e0b62e21 | 1509 | gfs2_glock_dq_uninit(&gh); |
240c6235 | 1510 | } |
23d828fc BP |
1511 | if (ret && ret != GLR_TRYFAILED && ret != -EROFS) |
1512 | fs_warn(sdp, "gfs2_evict_inode: %d\n", ret); | |
9e6e0a12 | 1513 | out: |
91b0abe3 | 1514 | truncate_inode_pages_final(&inode->i_data); |
2fba46a0 BP |
1515 | if (ip->i_qadata) |
1516 | gfs2_assert_warn(sdp, ip->i_qadata->qa_ref == 0); | |
1595548f | 1517 | gfs2_rs_delete(ip, NULL); |
45138990 | 1518 | gfs2_ordered_del_inode(ip); |
dbd5768f | 1519 | clear_inode(inode); |
17d539f0 | 1520 | gfs2_dir_hash_inval(ip); |
40e7e86e AG |
1521 | if (ip->i_gl) { |
1522 | glock_clear_object(ip->i_gl, ip); | |
1523 | wait_on_bit_io(&ip->i_flags, GIF_GLOP_PENDING, TASK_UNINTERRUPTIBLE); | |
1524 | gfs2_glock_add_to_lru(ip->i_gl); | |
1525 | gfs2_glock_put_eventually(ip->i_gl); | |
1526 | ip->i_gl = NULL; | |
1527 | } | |
6df9f9a2 | 1528 | if (gfs2_holder_initialized(&ip->i_iopen_gh)) { |
71c1b213 AG |
1529 | struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl; |
1530 | ||
1531 | glock_clear_object(gl, ip); | |
40e7e86e AG |
1532 | if (test_bit(HIF_HOLDER, &ip->i_iopen_gh.gh_iflags)) { |
1533 | ip->i_iopen_gh.gh_flags |= GL_NOCACHE; | |
1534 | gfs2_glock_dq(&ip->i_iopen_gh); | |
1535 | } | |
71c1b213 | 1536 | gfs2_glock_hold(gl); |
40e7e86e | 1537 | gfs2_holder_uninit(&ip->i_iopen_gh); |
71c1b213 | 1538 | gfs2_glock_put_eventually(gl); |
d5c1515c | 1539 | } |
9e6e0a12 SW |
1540 | } |
1541 | ||
1542 | static struct inode *gfs2_alloc_inode(struct super_block *sb) | |
1543 | { | |
1544 | struct gfs2_inode *ip; | |
1545 | ||
1546 | ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL); | |
d4031259 AG |
1547 | if (!ip) |
1548 | return NULL; | |
1549 | ip->i_flags = 0; | |
1550 | ip->i_gl = NULL; | |
40e7e86e | 1551 | gfs2_holder_mark_uninitialized(&ip->i_iopen_gh); |
d4031259 AG |
1552 | memset(&ip->i_res, 0, sizeof(ip->i_res)); |
1553 | RB_CLEAR_NODE(&ip->i_res.rs_node); | |
1554 | ip->i_rahead = 0; | |
9e6e0a12 SW |
1555 | return &ip->i_inode; |
1556 | } | |
1557 | ||
784494e1 | 1558 | static void gfs2_free_inode(struct inode *inode) |
9e6e0a12 | 1559 | { |
784494e1 | 1560 | kmem_cache_free(gfs2_inode_cachep, GFS2_I(inode)); |
fa0d7e3d NP |
1561 | } |
1562 | ||
97fd734b AD |
1563 | extern void free_local_statfs_inodes(struct gfs2_sbd *sdp) |
1564 | { | |
1565 | struct local_statfs_inode *lsi, *safe; | |
1566 | ||
1567 | /* Run through the statfs inodes list to iput and free memory */ | |
1568 | list_for_each_entry_safe(lsi, safe, &sdp->sd_sc_inodes_list, si_list) { | |
1569 | if (lsi->si_jid == sdp->sd_jdesc->jd_jid) | |
1570 | sdp->sd_sc_inode = NULL; /* belongs to this node */ | |
1571 | if (lsi->si_sc_inode) | |
1572 | iput(lsi->si_sc_inode); | |
1573 | list_del(&lsi->si_list); | |
1574 | kfree(lsi); | |
1575 | } | |
1576 | } | |
1577 | ||
1578 | extern struct inode *find_local_statfs_inode(struct gfs2_sbd *sdp, | |
1579 | unsigned int index) | |
1580 | { | |
1581 | struct local_statfs_inode *lsi; | |
1582 | ||
1583 | /* Return the local (per node) statfs inode in the | |
1584 | * sdp->sd_sc_inodes_list corresponding to the 'index'. */ | |
1585 | list_for_each_entry(lsi, &sdp->sd_sc_inodes_list, si_list) { | |
1586 | if (lsi->si_jid == index) | |
1587 | return lsi->si_sc_inode; | |
1588 | } | |
1589 | return NULL; | |
1590 | } | |
1591 | ||
9e6e0a12 SW |
1592 | const struct super_operations gfs2_super_ops = { |
1593 | .alloc_inode = gfs2_alloc_inode, | |
784494e1 | 1594 | .free_inode = gfs2_free_inode, |
9e6e0a12 | 1595 | .write_inode = gfs2_write_inode, |
ab9bbda0 | 1596 | .dirty_inode = gfs2_dirty_inode, |
d5c1515c | 1597 | .evict_inode = gfs2_evict_inode, |
9e6e0a12 | 1598 | .put_super = gfs2_put_super, |
9e6e0a12 | 1599 | .sync_fs = gfs2_sync_fs, |
2e60d768 BM |
1600 | .freeze_super = gfs2_freeze, |
1601 | .thaw_super = gfs2_unfreeze, | |
9e6e0a12 | 1602 | .statfs = gfs2_statfs, |
9e6e0a12 SW |
1603 | .drop_inode = gfs2_drop_inode, |
1604 | .show_options = gfs2_show_options, | |
1605 | }; | |
1606 |