]>
Commit | Line | Data |
---|---|---|
b3b94faa DT |
1 | /* |
2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. | |
3a8a9a10 | 3 | * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
b3b94faa DT |
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/pagemap.h> | |
fd88de56 | 16 | #include <linux/pagevec.h> |
9b124fbb | 17 | #include <linux/mpage.h> |
d1665e41 | 18 | #include <linux/fs.h> |
5c676f6d | 19 | #include <linux/gfs2_ondisk.h> |
b3b94faa DT |
20 | |
21 | #include "gfs2.h" | |
5c676f6d SW |
22 | #include "lm_interface.h" |
23 | #include "incore.h" | |
b3b94faa DT |
24 | #include "bmap.h" |
25 | #include "glock.h" | |
26 | #include "inode.h" | |
b3b94faa DT |
27 | #include "log.h" |
28 | #include "meta_io.h" | |
29 | #include "ops_address.h" | |
b3b94faa DT |
30 | #include "quota.h" |
31 | #include "trans.h" | |
18ec7d5c | 32 | #include "rgrp.h" |
61a30dcb | 33 | #include "ops_file.h" |
5c676f6d | 34 | #include "util.h" |
4340fe62 | 35 | #include "glops.h" |
b3b94faa | 36 | |
ba7f7290 SW |
37 | |
38 | static void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page, | |
39 | unsigned int from, unsigned int to) | |
40 | { | |
41 | struct buffer_head *head = page_buffers(page); | |
42 | unsigned int bsize = head->b_size; | |
43 | struct buffer_head *bh; | |
44 | unsigned int start, end; | |
45 | ||
46 | for (bh = head, start = 0; bh != head || !start; | |
47 | bh = bh->b_this_page, start = end) { | |
48 | end = start + bsize; | |
49 | if (end <= from || start >= to) | |
50 | continue; | |
51 | gfs2_trans_add_bh(ip->i_gl, bh, 0); | |
52 | } | |
53 | } | |
54 | ||
b3b94faa | 55 | /** |
4ff14670 | 56 | * gfs2_get_block - Fills in a buffer head with details about a block |
b3b94faa DT |
57 | * @inode: The inode |
58 | * @lblock: The block number to look up | |
59 | * @bh_result: The buffer head to return the result in | |
60 | * @create: Non-zero if we may add block to the file | |
61 | * | |
62 | * Returns: errno | |
63 | */ | |
64 | ||
4ff14670 SW |
65 | int gfs2_get_block(struct inode *inode, sector_t lblock, |
66 | struct buffer_head *bh_result, int create) | |
b3b94faa | 67 | { |
b3b94faa DT |
68 | int new = create; |
69 | uint64_t dblock; | |
70 | int error; | |
fd88de56 | 71 | int boundary; |
b3b94faa | 72 | |
fd88de56 | 73 | error = gfs2_block_map(inode, lblock, &new, &dblock, &boundary); |
b3b94faa DT |
74 | if (error) |
75 | return error; | |
76 | ||
77 | if (!dblock) | |
78 | return 0; | |
79 | ||
80 | map_bh(bh_result, inode->i_sb, dblock); | |
81 | if (new) | |
82 | set_buffer_new(bh_result); | |
fd88de56 SW |
83 | if (boundary) |
84 | set_buffer_boundary(bh_result); | |
b3b94faa DT |
85 | |
86 | return 0; | |
87 | } | |
88 | ||
89 | /** | |
90 | * get_block_noalloc - Fills in a buffer head with details about a block | |
91 | * @inode: The inode | |
92 | * @lblock: The block number to look up | |
93 | * @bh_result: The buffer head to return the result in | |
94 | * @create: Non-zero if we may add block to the file | |
95 | * | |
96 | * Returns: errno | |
97 | */ | |
98 | ||
99 | static int get_block_noalloc(struct inode *inode, sector_t lblock, | |
100 | struct buffer_head *bh_result, int create) | |
101 | { | |
b3b94faa DT |
102 | int new = 0; |
103 | uint64_t dblock; | |
104 | int error; | |
fd88de56 | 105 | int boundary; |
b3b94faa | 106 | |
fd88de56 | 107 | error = gfs2_block_map(inode, lblock, &new, &dblock, &boundary); |
b3b94faa DT |
108 | if (error) |
109 | return error; | |
110 | ||
111 | if (dblock) | |
112 | map_bh(bh_result, inode->i_sb, dblock); | |
feaa7bba | 113 | else if (gfs2_assert_withdraw(GFS2_SB(inode), !create)) |
b3b94faa | 114 | error = -EIO; |
fd88de56 SW |
115 | if (boundary) |
116 | set_buffer_boundary(bh_result); | |
b3b94faa DT |
117 | |
118 | return error; | |
119 | } | |
120 | ||
623d9355 SW |
121 | static int get_block_direct(struct inode *inode, sector_t lblock, |
122 | struct buffer_head *bh_result, int create) | |
123 | { | |
124 | int new = 0; | |
125 | u64 dblock; | |
126 | int error, boundary; | |
127 | ||
128 | error = gfs2_block_map(inode, lblock, &new, &dblock, &boundary); | |
129 | if (error) | |
130 | return error; | |
131 | ||
132 | if (dblock) { | |
133 | map_bh(bh_result, inode->i_sb, dblock); | |
134 | if (boundary) | |
135 | set_buffer_boundary(bh_result); | |
136 | } | |
137 | ||
138 | return 0; | |
139 | } | |
b3b94faa DT |
140 | /** |
141 | * gfs2_writepage - Write complete page | |
142 | * @page: Page to write | |
143 | * | |
144 | * Returns: errno | |
145 | * | |
18ec7d5c SW |
146 | * Some of this is copied from block_write_full_page() although we still |
147 | * call it to do most of the work. | |
b3b94faa DT |
148 | */ |
149 | ||
150 | static int gfs2_writepage(struct page *page, struct writeback_control *wbc) | |
151 | { | |
18ec7d5c | 152 | struct inode *inode = page->mapping->host; |
f4387149 SW |
153 | struct gfs2_inode *ip = GFS2_I(inode); |
154 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
18ec7d5c SW |
155 | loff_t i_size = i_size_read(inode); |
156 | pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT; | |
157 | unsigned offset; | |
b3b94faa | 158 | int error; |
18ec7d5c | 159 | int done_trans = 0; |
b3b94faa | 160 | |
b3b94faa DT |
161 | if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) { |
162 | unlock_page(page); | |
163 | return -EIO; | |
164 | } | |
5c676f6d | 165 | if (current->journal_info) |
18ec7d5c SW |
166 | goto out_ignore; |
167 | ||
168 | /* Is the page fully outside i_size? (truncate in progress) */ | |
169 | offset = i_size & (PAGE_CACHE_SIZE-1); | |
d2d7b8a2 | 170 | if (page->index > end_index || (page->index == end_index && !offset)) { |
18ec7d5c | 171 | page->mapping->a_ops->invalidatepage(page, 0); |
b3b94faa | 172 | unlock_page(page); |
18ec7d5c | 173 | return 0; /* don't care */ |
b3b94faa DT |
174 | } |
175 | ||
18ec7d5c SW |
176 | if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) { |
177 | error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0); | |
178 | if (error) | |
179 | goto out_ignore; | |
f4387149 SW |
180 | if (!page_has_buffers(page)) { |
181 | create_empty_buffers(page, inode->i_sb->s_blocksize, | |
182 | (1 << BH_Dirty)|(1 << BH_Uptodate)); | |
183 | } | |
18ec7d5c SW |
184 | gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1); |
185 | done_trans = 1; | |
186 | } | |
18ec7d5c SW |
187 | error = block_write_full_page(page, get_block_noalloc, wbc); |
188 | if (done_trans) | |
189 | gfs2_trans_end(sdp); | |
b3b94faa | 190 | gfs2_meta_cache_flush(ip); |
b3b94faa | 191 | return error; |
18ec7d5c SW |
192 | |
193 | out_ignore: | |
194 | redirty_page_for_writepage(wbc, page); | |
195 | unlock_page(page); | |
196 | return 0; | |
b3b94faa DT |
197 | } |
198 | ||
fd88de56 SW |
199 | static int zero_readpage(struct page *page) |
200 | { | |
201 | void *kaddr; | |
202 | ||
203 | kaddr = kmap_atomic(page, KM_USER0); | |
204 | memset(kaddr, 0, PAGE_CACHE_SIZE); | |
205 | kunmap_atomic(page, KM_USER0); | |
206 | ||
207 | SetPageUptodate(page); | |
208 | ||
209 | return 0; | |
210 | } | |
211 | ||
b3b94faa DT |
212 | /** |
213 | * stuffed_readpage - Fill in a Linux page with stuffed file data | |
214 | * @ip: the inode | |
215 | * @page: the page | |
216 | * | |
217 | * Returns: errno | |
218 | */ | |
219 | ||
220 | static int stuffed_readpage(struct gfs2_inode *ip, struct page *page) | |
221 | { | |
222 | struct buffer_head *dibh; | |
223 | void *kaddr; | |
224 | int error; | |
225 | ||
fd88de56 SW |
226 | /* Only the first page of a stuffed file might contain data */ |
227 | if (unlikely(page->index)) | |
228 | return zero_readpage(page); | |
229 | ||
b3b94faa DT |
230 | error = gfs2_meta_inode_buffer(ip, &dibh); |
231 | if (error) | |
232 | return error; | |
233 | ||
5c4e9e03 | 234 | kaddr = kmap_atomic(page, KM_USER0); |
fd88de56 | 235 | memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), |
b3b94faa | 236 | ip->i_di.di_size); |
fd88de56 | 237 | memset(kaddr + ip->i_di.di_size, 0, PAGE_CACHE_SIZE - ip->i_di.di_size); |
5c4e9e03 | 238 | kunmap_atomic(page, KM_USER0); |
b3b94faa DT |
239 | |
240 | brelse(dibh); | |
241 | ||
242 | SetPageUptodate(page); | |
243 | ||
244 | return 0; | |
245 | } | |
246 | ||
b3b94faa | 247 | |
b3b94faa DT |
248 | /** |
249 | * gfs2_readpage - readpage with locking | |
18ec7d5c SW |
250 | * @file: The file to read a page for. N.B. This may be NULL if we are |
251 | * reading an internal file. | |
b3b94faa DT |
252 | * @page: The page to read |
253 | * | |
254 | * Returns: errno | |
255 | */ | |
256 | ||
257 | static int gfs2_readpage(struct file *file, struct page *page) | |
258 | { | |
feaa7bba SW |
259 | struct gfs2_inode *ip = GFS2_I(page->mapping->host); |
260 | struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host); | |
18ec7d5c | 261 | struct gfs2_holder gh; |
b3b94faa | 262 | int error; |
59a1cc6b | 263 | int do_unlock = 0; |
b3b94faa | 264 | |
fd88de56 | 265 | if (likely(file != &gfs2_internal_file_sentinal)) { |
59a1cc6b SW |
266 | if (file) { |
267 | struct gfs2_file *gf = file->private_data; | |
268 | if (test_bit(GFF_EXLOCK, &gf->f_flags)) | |
269 | goto skip_lock; | |
270 | } | |
fe1bdedc | 271 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|GL_AOP, &gh); |
59a1cc6b | 272 | do_unlock = 1; |
61a30dcb | 273 | error = gfs2_glock_nq_m_atime(1, &gh); |
fd88de56 | 274 | if (unlikely(error)) |
61a30dcb SW |
275 | goto out_unlock; |
276 | } | |
b3b94faa | 277 | |
59a1cc6b | 278 | skip_lock: |
18ec7d5c | 279 | if (gfs2_is_stuffed(ip)) { |
fd88de56 SW |
280 | error = stuffed_readpage(ip, page); |
281 | unlock_page(page); | |
b3b94faa | 282 | } else |
18ec7d5c | 283 | error = mpage_readpage(page, gfs2_get_block); |
b3b94faa DT |
284 | |
285 | if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) | |
286 | error = -EIO; | |
287 | ||
61a30dcb SW |
288 | if (file != &gfs2_internal_file_sentinal) { |
289 | gfs2_glock_dq_m(1, &gh); | |
290 | gfs2_holder_uninit(&gh); | |
291 | } | |
18ec7d5c | 292 | out: |
b3b94faa | 293 | return error; |
18ec7d5c SW |
294 | out_unlock: |
295 | unlock_page(page); | |
59a1cc6b | 296 | if (do_unlock) |
fd88de56 SW |
297 | gfs2_holder_uninit(&gh); |
298 | goto out; | |
299 | } | |
300 | ||
fd88de56 SW |
301 | /** |
302 | * gfs2_readpages - Read a bunch of pages at once | |
303 | * | |
304 | * Some notes: | |
305 | * 1. This is only for readahead, so we can simply ignore any things | |
306 | * which are slightly inconvenient (such as locking conflicts between | |
307 | * the page lock and the glock) and return having done no I/O. Its | |
308 | * obviously not something we'd want to do on too regular a basis. | |
309 | * Any I/O we ignore at this time will be done via readpage later. | |
310 | * 2. We have to handle stuffed files here too. | |
311 | * 3. mpage_readpages() does most of the heavy lifting in the common case. | |
312 | * 4. gfs2_get_block() is relied upon to set BH_Boundary in the right places. | |
313 | * 5. We use LM_FLAG_TRY_1CB here, effectively we then have lock-ahead as | |
314 | * well as read-ahead. | |
315 | */ | |
316 | static int gfs2_readpages(struct file *file, struct address_space *mapping, | |
317 | struct list_head *pages, unsigned nr_pages) | |
318 | { | |
319 | struct inode *inode = mapping->host; | |
feaa7bba SW |
320 | struct gfs2_inode *ip = GFS2_I(inode); |
321 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
fd88de56 SW |
322 | struct gfs2_holder gh; |
323 | unsigned page_idx; | |
324 | int ret; | |
59a1cc6b | 325 | int do_unlock = 0; |
fd88de56 SW |
326 | |
327 | if (likely(file != &gfs2_internal_file_sentinal)) { | |
59a1cc6b SW |
328 | if (file) { |
329 | struct gfs2_file *gf = file->private_data; | |
330 | if (test_bit(GFF_EXLOCK, &gf->f_flags)) | |
331 | goto skip_lock; | |
332 | } | |
fd88de56 SW |
333 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, |
334 | LM_FLAG_TRY_1CB|GL_ATIME|GL_AOP, &gh); | |
59a1cc6b | 335 | do_unlock = 1; |
fd88de56 SW |
336 | ret = gfs2_glock_nq_m_atime(1, &gh); |
337 | if (ret == GLR_TRYFAILED) | |
338 | goto out_noerror; | |
339 | if (unlikely(ret)) | |
340 | goto out_unlock; | |
341 | } | |
59a1cc6b | 342 | skip_lock: |
fd88de56 SW |
343 | if (gfs2_is_stuffed(ip)) { |
344 | struct pagevec lru_pvec; | |
345 | pagevec_init(&lru_pvec, 0); | |
346 | for (page_idx = 0; page_idx < nr_pages; page_idx++) { | |
ffeb874b SW |
347 | struct page *page = list_entry(pages->prev, struct page, lru); |
348 | prefetchw(&page->flags); | |
fd88de56 SW |
349 | list_del(&page->lru); |
350 | if (!add_to_page_cache(page, mapping, | |
351 | page->index, GFP_KERNEL)) { | |
352 | ret = stuffed_readpage(ip, page); | |
353 | unlock_page(page); | |
354 | if (!pagevec_add(&lru_pvec, page)) | |
355 | __pagevec_lru_add(&lru_pvec); | |
ffeb874b SW |
356 | } else { |
357 | page_cache_release(page); | |
fd88de56 | 358 | } |
fd88de56 SW |
359 | } |
360 | pagevec_lru_add(&lru_pvec); | |
361 | ret = 0; | |
362 | } else { | |
363 | /* What we really want to do .... */ | |
364 | ret = mpage_readpages(mapping, pages, nr_pages, gfs2_get_block); | |
365 | } | |
366 | ||
59a1cc6b | 367 | if (do_unlock) { |
fd88de56 SW |
368 | gfs2_glock_dq_m(1, &gh); |
369 | gfs2_holder_uninit(&gh); | |
370 | } | |
371 | out: | |
372 | if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) | |
373 | ret = -EIO; | |
374 | return ret; | |
375 | out_noerror: | |
376 | ret = 0; | |
377 | out_unlock: | |
378 | /* unlock all pages, we can't do any I/O right now */ | |
379 | for (page_idx = 0; page_idx < nr_pages; page_idx++) { | |
ffeb874b | 380 | struct page *page = list_entry(pages->prev, struct page, lru); |
fd88de56 SW |
381 | list_del(&page->lru); |
382 | unlock_page(page); | |
383 | page_cache_release(page); | |
384 | } | |
59a1cc6b | 385 | if (do_unlock) |
fd88de56 | 386 | gfs2_holder_uninit(&gh); |
18ec7d5c | 387 | goto out; |
b3b94faa DT |
388 | } |
389 | ||
390 | /** | |
391 | * gfs2_prepare_write - Prepare to write a page to a file | |
392 | * @file: The file to write to | |
393 | * @page: The page which is to be prepared for writing | |
394 | * @from: From (byte range within page) | |
395 | * @to: To (byte range within page) | |
396 | * | |
397 | * Returns: errno | |
398 | */ | |
399 | ||
400 | static int gfs2_prepare_write(struct file *file, struct page *page, | |
401 | unsigned from, unsigned to) | |
402 | { | |
feaa7bba SW |
403 | struct gfs2_inode *ip = GFS2_I(page->mapping->host); |
404 | struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host); | |
18ec7d5c SW |
405 | unsigned int data_blocks, ind_blocks, rblocks; |
406 | int alloc_required; | |
b3b94faa | 407 | int error = 0; |
18ec7d5c SW |
408 | loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + from; |
409 | loff_t end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to; | |
410 | struct gfs2_alloc *al; | |
b3b94faa | 411 | |
fe1bdedc | 412 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME|GL_AOP, &ip->i_gh); |
18ec7d5c SW |
413 | error = gfs2_glock_nq_m_atime(1, &ip->i_gh); |
414 | if (error) | |
415 | goto out_uninit; | |
b3b94faa | 416 | |
18ec7d5c SW |
417 | gfs2_write_calc_reserv(ip, to - from, &data_blocks, &ind_blocks); |
418 | ||
419 | error = gfs2_write_alloc_required(ip, pos, from - to, &alloc_required); | |
420 | if (error) | |
421 | goto out_unlock; | |
b3b94faa | 422 | |
18ec7d5c SW |
423 | |
424 | if (alloc_required) { | |
425 | al = gfs2_alloc_get(ip); | |
426 | ||
427 | error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); | |
428 | if (error) | |
429 | goto out_alloc_put; | |
430 | ||
431 | error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid); | |
432 | if (error) | |
433 | goto out_qunlock; | |
434 | ||
435 | al->al_requested = data_blocks + ind_blocks; | |
436 | error = gfs2_inplace_reserve(ip); | |
437 | if (error) | |
438 | goto out_qunlock; | |
439 | } | |
440 | ||
441 | rblocks = RES_DINODE + ind_blocks; | |
442 | if (gfs2_is_jdata(ip)) | |
443 | rblocks += data_blocks ? data_blocks : 1; | |
444 | if (ind_blocks || data_blocks) | |
445 | rblocks += RES_STATFS + RES_QUOTA; | |
446 | ||
447 | error = gfs2_trans_begin(sdp, rblocks, 0); | |
448 | if (error) | |
449 | goto out; | |
450 | ||
451 | if (gfs2_is_stuffed(ip)) { | |
452 | if (end > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) { | |
f25ef0c1 | 453 | error = gfs2_unstuff_dinode(ip, page); |
5c4e9e03 SW |
454 | if (error == 0) |
455 | goto prepare_write; | |
456 | } else if (!PageUptodate(page)) | |
b3b94faa | 457 | error = stuffed_readpage(ip, page); |
5c4e9e03 | 458 | goto out; |
18ec7d5c SW |
459 | } |
460 | ||
5c4e9e03 | 461 | prepare_write: |
18ec7d5c SW |
462 | error = block_prepare_write(page, from, to, gfs2_get_block); |
463 | ||
464 | out: | |
465 | if (error) { | |
466 | gfs2_trans_end(sdp); | |
467 | if (alloc_required) { | |
468 | gfs2_inplace_release(ip); | |
469 | out_qunlock: | |
470 | gfs2_quota_unlock(ip); | |
471 | out_alloc_put: | |
472 | gfs2_alloc_put(ip); | |
473 | } | |
474 | out_unlock: | |
475 | gfs2_glock_dq_m(1, &ip->i_gh); | |
476 | out_uninit: | |
477 | gfs2_holder_uninit(&ip->i_gh); | |
478 | } | |
b3b94faa DT |
479 | |
480 | return error; | |
481 | } | |
482 | ||
483 | /** | |
484 | * gfs2_commit_write - Commit write to a file | |
485 | * @file: The file to write to | |
486 | * @page: The page containing the data | |
487 | * @from: From (byte range within page) | |
488 | * @to: To (byte range within page) | |
489 | * | |
490 | * Returns: errno | |
491 | */ | |
492 | ||
493 | static int gfs2_commit_write(struct file *file, struct page *page, | |
494 | unsigned from, unsigned to) | |
495 | { | |
496 | struct inode *inode = page->mapping->host; | |
feaa7bba SW |
497 | struct gfs2_inode *ip = GFS2_I(inode); |
498 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
18ec7d5c SW |
499 | int error = -EOPNOTSUPP; |
500 | struct buffer_head *dibh; | |
501 | struct gfs2_alloc *al = &ip->i_alloc;; | |
b3b94faa | 502 | |
18ec7d5c SW |
503 | if (gfs2_assert_withdraw(sdp, gfs2_glock_is_locked_by_me(ip->i_gl))) |
504 | goto fail_nounlock; | |
505 | ||
506 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
507 | if (error) | |
508 | goto fail_endtrans; | |
509 | ||
510 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); | |
511 | ||
b3b94faa | 512 | if (gfs2_is_stuffed(ip)) { |
b3b94faa DT |
513 | uint64_t file_size; |
514 | void *kaddr; | |
515 | ||
516 | file_size = ((uint64_t)page->index << PAGE_CACHE_SHIFT) + to; | |
517 | ||
18ec7d5c | 518 | kaddr = kmap_atomic(page, KM_USER0); |
b3b94faa | 519 | memcpy(dibh->b_data + sizeof(struct gfs2_dinode) + from, |
18ec7d5c SW |
520 | (char *)kaddr + from, to - from); |
521 | kunmap_atomic(page, KM_USER0); | |
b3b94faa DT |
522 | |
523 | SetPageUptodate(page); | |
524 | ||
525 | if (inode->i_size < file_size) | |
526 | i_size_write(inode, file_size); | |
527 | } else { | |
568f4c96 SW |
528 | if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || |
529 | gfs2_is_jdata(ip)) | |
257f9b4e | 530 | gfs2_page_add_databufs(ip, page, from, to); |
b3b94faa DT |
531 | error = generic_commit_write(file, page, from, to); |
532 | if (error) | |
533 | goto fail; | |
534 | } | |
535 | ||
18ec7d5c SW |
536 | if (ip->i_di.di_size < inode->i_size) |
537 | ip->i_di.di_size = inode->i_size; | |
538 | ||
539 | gfs2_dinode_out(&ip->i_di, dibh->b_data); | |
540 | brelse(dibh); | |
541 | gfs2_trans_end(sdp); | |
542 | if (al->al_requested) { | |
543 | gfs2_inplace_release(ip); | |
544 | gfs2_quota_unlock(ip); | |
545 | gfs2_alloc_put(ip); | |
546 | } | |
547 | gfs2_glock_dq_m(1, &ip->i_gh); | |
548 | gfs2_holder_uninit(&ip->i_gh); | |
b3b94faa DT |
549 | return 0; |
550 | ||
18ec7d5c SW |
551 | fail: |
552 | brelse(dibh); | |
553 | fail_endtrans: | |
554 | gfs2_trans_end(sdp); | |
555 | if (al->al_requested) { | |
556 | gfs2_inplace_release(ip); | |
557 | gfs2_quota_unlock(ip); | |
558 | gfs2_alloc_put(ip); | |
559 | } | |
560 | gfs2_glock_dq_m(1, &ip->i_gh); | |
561 | gfs2_holder_uninit(&ip->i_gh); | |
562 | fail_nounlock: | |
b3b94faa | 563 | ClearPageUptodate(page); |
b3b94faa DT |
564 | return error; |
565 | } | |
566 | ||
567 | /** | |
568 | * gfs2_bmap - Block map function | |
569 | * @mapping: Address space info | |
570 | * @lblock: The block to map | |
571 | * | |
572 | * Returns: The disk address for the block or 0 on hole or error | |
573 | */ | |
574 | ||
575 | static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock) | |
576 | { | |
feaa7bba | 577 | struct gfs2_inode *ip = GFS2_I(mapping->host); |
b3b94faa DT |
578 | struct gfs2_holder i_gh; |
579 | sector_t dblock = 0; | |
580 | int error; | |
581 | ||
b3b94faa DT |
582 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh); |
583 | if (error) | |
584 | return 0; | |
585 | ||
586 | if (!gfs2_is_stuffed(ip)) | |
4ff14670 | 587 | dblock = generic_block_bmap(mapping, lblock, gfs2_get_block); |
b3b94faa DT |
588 | |
589 | gfs2_glock_dq_uninit(&i_gh); | |
590 | ||
591 | return dblock; | |
592 | } | |
593 | ||
594 | static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh) | |
595 | { | |
64fb4eb7 | 596 | struct gfs2_bufdata *bd; |
b3b94faa DT |
597 | |
598 | gfs2_log_lock(sdp); | |
5c676f6d | 599 | bd = bh->b_private; |
64fb4eb7 SW |
600 | if (bd) { |
601 | bd->bd_bh = NULL; | |
5c676f6d | 602 | bh->b_private = NULL; |
15d00c0b SW |
603 | } |
604 | gfs2_log_unlock(sdp); | |
b3b94faa DT |
605 | |
606 | lock_buffer(bh); | |
607 | clear_buffer_dirty(bh); | |
608 | bh->b_bdev = NULL; | |
609 | clear_buffer_mapped(bh); | |
610 | clear_buffer_req(bh); | |
611 | clear_buffer_new(bh); | |
612 | clear_buffer_delay(bh); | |
613 | unlock_buffer(bh); | |
614 | } | |
615 | ||
8628de05 | 616 | static void gfs2_invalidatepage(struct page *page, unsigned long offset) |
b3b94faa | 617 | { |
15d00c0b | 618 | struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host); |
b3b94faa DT |
619 | struct buffer_head *head, *bh, *next; |
620 | unsigned int curr_off = 0; | |
b3b94faa DT |
621 | |
622 | BUG_ON(!PageLocked(page)); | |
623 | if (!page_has_buffers(page)) | |
8628de05 | 624 | return; |
b3b94faa DT |
625 | |
626 | bh = head = page_buffers(page); | |
627 | do { | |
628 | unsigned int next_off = curr_off + bh->b_size; | |
629 | next = bh->b_this_page; | |
630 | ||
631 | if (offset <= curr_off) | |
632 | discard_buffer(sdp, bh); | |
633 | ||
634 | curr_off = next_off; | |
635 | bh = next; | |
636 | } while (bh != head); | |
637 | ||
638 | if (!offset) | |
8628de05 | 639 | try_to_release_page(page, 0); |
b3b94faa | 640 | |
8628de05 | 641 | return; |
b3b94faa DT |
642 | } |
643 | ||
a9e5f4d0 SW |
644 | static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb, |
645 | const struct iovec *iov, loff_t offset, | |
646 | unsigned long nr_segs) | |
d1665e41 SW |
647 | { |
648 | struct file *file = iocb->ki_filp; | |
649 | struct inode *inode = file->f_mapping->host; | |
feaa7bba | 650 | struct gfs2_inode *ip = GFS2_I(inode); |
d1665e41 SW |
651 | struct gfs2_holder gh; |
652 | int rv; | |
653 | ||
a9e5f4d0 SW |
654 | if (rw == READ) |
655 | mutex_lock(&inode->i_mutex); | |
d1665e41 | 656 | /* |
a9e5f4d0 | 657 | * Shared lock, even if its a write, since we do no allocation |
d1665e41 SW |
658 | * on this path. All we need change is atime. |
659 | */ | |
660 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh); | |
661 | rv = gfs2_glock_nq_m_atime(1, &gh); | |
662 | if (rv) | |
663 | goto out; | |
664 | ||
a9e5f4d0 SW |
665 | if (offset > i_size_read(inode)) |
666 | goto out; | |
667 | ||
d1665e41 SW |
668 | /* |
669 | * Should we return an error here? I can't see that O_DIRECT for | |
670 | * a journaled file makes any sense. For now we'll silently fall | |
671 | * back to buffered I/O, likewise we do the same for stuffed | |
672 | * files since they are (a) small and (b) unaligned. | |
673 | */ | |
674 | if (gfs2_is_jdata(ip)) | |
675 | goto out; | |
676 | ||
677 | if (gfs2_is_stuffed(ip)) | |
678 | goto out; | |
679 | ||
a9e5f4d0 SW |
680 | rv = blockdev_direct_IO_own_locking(rw, iocb, inode, |
681 | inode->i_sb->s_bdev, | |
682 | iov, offset, nr_segs, | |
623d9355 | 683 | get_block_direct, NULL); |
d1665e41 SW |
684 | out: |
685 | gfs2_glock_dq_m(1, &gh); | |
686 | gfs2_holder_uninit(&gh); | |
a9e5f4d0 SW |
687 | if (rw == READ) |
688 | mutex_unlock(&inode->i_mutex); | |
d1665e41 SW |
689 | |
690 | return rv; | |
691 | } | |
692 | ||
4340fe62 SW |
693 | /** |
694 | * stuck_releasepage - We're stuck in gfs2_releasepage(). Print stuff out. | |
695 | * @bh: the buffer we're stuck on | |
696 | * | |
697 | */ | |
698 | ||
699 | static void stuck_releasepage(struct buffer_head *bh) | |
700 | { | |
701 | struct inode *inode = bh->b_page->mapping->host; | |
702 | struct gfs2_sbd *sdp = inode->i_sb->s_fs_info; | |
703 | struct gfs2_bufdata *bd = bh->b_private; | |
704 | struct gfs2_glock *gl; | |
166afccd SW |
705 | static unsigned limit = 0; |
706 | ||
707 | if (limit++ > 3) | |
708 | return; | |
4340fe62 SW |
709 | |
710 | fs_warn(sdp, "stuck in gfs2_releasepage() %p\n", inode); | |
711 | fs_warn(sdp, "blkno = %llu, bh->b_count = %d\n", | |
712 | (unsigned long long)bh->b_blocknr, atomic_read(&bh->b_count)); | |
713 | fs_warn(sdp, "pinned = %u\n", buffer_pinned(bh)); | |
714 | fs_warn(sdp, "bh->b_private = %s\n", (bd) ? "!NULL" : "NULL"); | |
715 | ||
716 | if (!bd) | |
717 | return; | |
718 | ||
719 | gl = bd->bd_gl; | |
720 | ||
721 | fs_warn(sdp, "gl = (%u, %llu)\n", | |
722 | gl->gl_name.ln_type, (unsigned long long)gl->gl_name.ln_number); | |
723 | ||
724 | fs_warn(sdp, "bd_list_tr = %s, bd_le.le_list = %s\n", | |
725 | (list_empty(&bd->bd_list_tr)) ? "no" : "yes", | |
726 | (list_empty(&bd->bd_le.le_list)) ? "no" : "yes"); | |
727 | ||
728 | if (gl->gl_ops == &gfs2_inode_glops) { | |
729 | struct gfs2_inode *ip = gl->gl_object; | |
730 | unsigned int x; | |
731 | ||
732 | if (!ip) | |
733 | return; | |
734 | ||
735 | fs_warn(sdp, "ip = %llu %llu\n", | |
736 | (unsigned long long)ip->i_num.no_formal_ino, | |
737 | (unsigned long long)ip->i_num.no_addr); | |
738 | ||
739 | for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) | |
740 | fs_warn(sdp, "ip->i_cache[%u] = %s\n", | |
741 | x, (ip->i_cache[x]) ? "!NULL" : "NULL"); | |
742 | } | |
743 | } | |
744 | ||
745 | /** | |
623d9355 | 746 | * gfs2_releasepage - free the metadata associated with a page |
4340fe62 SW |
747 | * @page: the page that's being released |
748 | * @gfp_mask: passed from Linux VFS, ignored by us | |
749 | * | |
750 | * Call try_to_free_buffers() if the buffers in this page can be | |
751 | * released. | |
752 | * | |
753 | * Returns: 0 | |
754 | */ | |
755 | ||
756 | int gfs2_releasepage(struct page *page, gfp_t gfp_mask) | |
757 | { | |
758 | struct inode *aspace = page->mapping->host; | |
759 | struct gfs2_sbd *sdp = aspace->i_sb->s_fs_info; | |
760 | struct buffer_head *bh, *head; | |
761 | struct gfs2_bufdata *bd; | |
166afccd | 762 | unsigned long t = jiffies + gfs2_tune_get(sdp, gt_stall_secs) * HZ; |
4340fe62 SW |
763 | |
764 | if (!page_has_buffers(page)) | |
765 | goto out; | |
766 | ||
767 | head = bh = page_buffers(page); | |
768 | do { | |
4340fe62 | 769 | while (atomic_read(&bh->b_count)) { |
166afccd SW |
770 | if (!atomic_read(&aspace->i_writecount)) |
771 | return 0; | |
772 | ||
773 | if (time_after_eq(jiffies, t)) { | |
774 | stuck_releasepage(bh); | |
775 | /* should we withdraw here? */ | |
776 | return 0; | |
4340fe62 SW |
777 | } |
778 | ||
166afccd | 779 | yield(); |
4340fe62 SW |
780 | } |
781 | ||
782 | gfs2_assert_warn(sdp, !buffer_pinned(bh)); | |
623d9355 | 783 | gfs2_assert_warn(sdp, !buffer_dirty(bh)); |
4340fe62 | 784 | |
623d9355 | 785 | gfs2_log_lock(sdp); |
4340fe62 SW |
786 | bd = bh->b_private; |
787 | if (bd) { | |
788 | gfs2_assert_warn(sdp, bd->bd_bh == bh); | |
789 | gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr)); | |
4340fe62 | 790 | gfs2_assert_warn(sdp, !bd->bd_ail); |
623d9355 SW |
791 | bd->bd_bh = NULL; |
792 | if (!list_empty(&bd->bd_le.le_list)) | |
793 | bd = NULL; | |
4340fe62 SW |
794 | bh->b_private = NULL; |
795 | } | |
623d9355 SW |
796 | gfs2_log_unlock(sdp); |
797 | if (bd) | |
798 | kmem_cache_free(gfs2_bufdata_cachep, bd); | |
4340fe62 SW |
799 | |
800 | bh = bh->b_this_page; | |
166afccd | 801 | } while (bh != head); |
4340fe62 | 802 | |
a9e5f4d0 | 803 | out: |
4340fe62 SW |
804 | return try_to_free_buffers(page); |
805 | } | |
806 | ||
66de045d | 807 | const struct address_space_operations gfs2_file_aops = { |
b3b94faa DT |
808 | .writepage = gfs2_writepage, |
809 | .readpage = gfs2_readpage, | |
fd88de56 | 810 | .readpages = gfs2_readpages, |
b3b94faa DT |
811 | .sync_page = block_sync_page, |
812 | .prepare_write = gfs2_prepare_write, | |
813 | .commit_write = gfs2_commit_write, | |
814 | .bmap = gfs2_bmap, | |
815 | .invalidatepage = gfs2_invalidatepage, | |
4340fe62 | 816 | .releasepage = gfs2_releasepage, |
b3b94faa DT |
817 | .direct_IO = gfs2_direct_IO, |
818 | }; | |
819 |