]> err.no Git - linux-2.6/blob - fs/gfs2/ops_address.c
[GFS2] Fix for lock recursion problem for internal files
[linux-2.6] / fs / gfs2 / ops_address.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2005 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/pagemap.h>
16 #include <linux/mpage.h>
17 #include <linux/fs.h>
18 #include <asm/semaphore.h>
19
20 #include "gfs2.h"
21 #include "bmap.h"
22 #include "glock.h"
23 #include "inode.h"
24 #include "log.h"
25 #include "meta_io.h"
26 #include "ops_address.h"
27 #include "page.h"
28 #include "quota.h"
29 #include "trans.h"
30 #include "rgrp.h"
31 #include "ops_file.h"
32
33 /**
34  * gfs2_get_block - Fills in a buffer head with details about a block
35  * @inode: The inode
36  * @lblock: The block number to look up
37  * @bh_result: The buffer head to return the result in
38  * @create: Non-zero if we may add block to the file
39  *
40  * Returns: errno
41  */
42
43 int gfs2_get_block(struct inode *inode, sector_t lblock,
44                    struct buffer_head *bh_result, int create)
45 {
46         struct gfs2_inode *ip = get_v2ip(inode);
47         int new = create;
48         uint64_t dblock;
49         int error;
50
51         error = gfs2_block_map(ip, lblock, &new, &dblock, NULL);
52         if (error)
53                 return error;
54
55         if (!dblock)
56                 return 0;
57
58         map_bh(bh_result, inode->i_sb, dblock);
59         if (new)
60                 set_buffer_new(bh_result);
61
62         return 0;
63 }
64
65 /**
66  * get_block_noalloc - Fills in a buffer head with details about a block
67  * @inode: The inode
68  * @lblock: The block number to look up
69  * @bh_result: The buffer head to return the result in
70  * @create: Non-zero if we may add block to the file
71  *
72  * Returns: errno
73  */
74
75 static int get_block_noalloc(struct inode *inode, sector_t lblock,
76                              struct buffer_head *bh_result, int create)
77 {
78         struct gfs2_inode *ip = get_v2ip(inode);
79         int new = 0;
80         uint64_t dblock;
81         int error;
82
83         error = gfs2_block_map(ip, lblock, &new, &dblock, NULL);
84         if (error)
85                 return error;
86
87         if (dblock)
88                 map_bh(bh_result, inode->i_sb, dblock);
89         else if (gfs2_assert_withdraw(ip->i_sbd, !create))
90                 error = -EIO;
91
92         return error;
93 }
94
95 static int get_blocks(struct inode *inode, sector_t lblock,
96                       unsigned long max_blocks, struct buffer_head *bh_result,
97                       int create)
98 {
99         struct gfs2_inode *ip = get_v2ip(inode);
100         int new = create;
101         uint64_t dblock;
102         uint32_t extlen;
103         int error;
104
105         error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen);
106         if (error)
107                 return error;
108
109         if (!dblock)
110                 return 0;
111
112         map_bh(bh_result, inode->i_sb, dblock);
113         if (new)
114                 set_buffer_new(bh_result);
115
116         if (extlen > max_blocks)
117                 extlen = max_blocks;
118         bh_result->b_size = extlen << inode->i_blkbits;
119
120         return 0;
121 }
122
123 static int get_blocks_noalloc(struct inode *inode, sector_t lblock,
124                               unsigned long max_blocks,
125                               struct buffer_head *bh_result, int create)
126 {
127         struct gfs2_inode *ip = get_v2ip(inode);
128         int new = 0;
129         uint64_t dblock;
130         uint32_t extlen;
131         int error;
132
133         error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen);
134         if (error)
135                 return error;
136
137         if (dblock) {
138                 map_bh(bh_result, inode->i_sb, dblock);
139                 if (extlen > max_blocks)
140                         extlen = max_blocks;
141                 bh_result->b_size = extlen << inode->i_blkbits;
142         } else if (gfs2_assert_withdraw(ip->i_sbd, !create))
143                 error = -EIO;
144
145         return error;
146 }
147
148 /**
149  * gfs2_writepage - Write complete page
150  * @page: Page to write
151  *
152  * Returns: errno
153  *
154  * Some of this is copied from block_write_full_page() although we still
155  * call it to do most of the work.
156  */
157
158 static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
159 {
160         struct inode *inode = page->mapping->host;
161         struct gfs2_inode *ip = get_v2ip(page->mapping->host);
162         struct gfs2_sbd *sdp = ip->i_sbd;
163         loff_t i_size = i_size_read(inode);
164         pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
165         unsigned offset;
166         int error;
167         int done_trans = 0;
168
169         atomic_inc(&sdp->sd_ops_address);
170         if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) {
171                 unlock_page(page);
172                 return -EIO;
173         }
174         if (get_transaction)
175                 goto out_ignore;
176
177         /* Is the page fully outside i_size? (truncate in progress) */
178         offset = i_size & (PAGE_CACHE_SIZE-1);
179         if (page->index >= end_index+1 || !offset) {
180                 page->mapping->a_ops->invalidatepage(page, 0);
181                 unlock_page(page);
182                 return 0; /* don't care */
183         }
184
185         if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) {
186                 error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
187                 if (error)
188                         goto out_ignore;
189                 gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
190                 done_trans = 1;
191         }
192
193         error = block_write_full_page(page, get_block_noalloc, wbc);
194         if (done_trans)
195                 gfs2_trans_end(sdp);
196         gfs2_meta_cache_flush(ip);
197         return error;
198
199 out_ignore:
200         redirty_page_for_writepage(wbc, page);
201         unlock_page(page);
202         return 0;
203 }
204
205 /**
206  * stuffed_readpage - Fill in a Linux page with stuffed file data
207  * @ip: the inode
208  * @page: the page
209  *
210  * Returns: errno
211  */
212
213 static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
214 {
215         struct buffer_head *dibh;
216         void *kaddr;
217         int error;
218
219         error = gfs2_meta_inode_buffer(ip, &dibh);
220         if (error)
221                 return error;
222
223         kaddr = kmap(page);
224         memcpy((char *)kaddr,
225                dibh->b_data + sizeof(struct gfs2_dinode),
226                ip->i_di.di_size);
227         memset((char *)kaddr + ip->i_di.di_size,
228                0,
229                PAGE_CACHE_SIZE - ip->i_di.di_size);
230         kunmap(page);
231
232         brelse(dibh);
233
234         SetPageUptodate(page);
235
236         return 0;
237 }
238
239 static int zero_readpage(struct page *page)
240 {
241         void *kaddr;
242
243         kaddr = kmap(page);
244         memset(kaddr, 0, PAGE_CACHE_SIZE);
245         kunmap(page);
246
247         SetPageUptodate(page);
248         unlock_page(page);
249
250         return 0;
251 }
252
253 /**
254  * gfs2_readpage - readpage with locking
255  * @file: The file to read a page for. N.B. This may be NULL if we are
256  * reading an internal file.
257  * @page: The page to read
258  *
259  * Returns: errno
260  */
261
262 static int gfs2_readpage(struct file *file, struct page *page)
263 {
264         struct gfs2_inode *ip = get_v2ip(page->mapping->host);
265         struct gfs2_sbd *sdp = ip->i_sbd;
266         struct gfs2_holder gh;
267         int error;
268
269         atomic_inc(&sdp->sd_ops_address);
270
271         if (file != &gfs2_internal_file_sentinal) {
272                 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
273                 error = gfs2_glock_nq_m_atime(1, &gh);
274                 if (error)
275                         goto out_unlock;
276         }
277
278         if (gfs2_is_stuffed(ip)) {
279                 if (!page->index) {
280                         error = stuffed_readpage(ip, page);
281                         unlock_page(page);
282                 } else
283                         error = zero_readpage(page);
284         } else
285                 error = mpage_readpage(page, gfs2_get_block);
286
287         if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
288                 error = -EIO;
289
290         if (file != &gfs2_internal_file_sentinal) {
291                 gfs2_glock_dq_m(1, &gh);
292                 gfs2_holder_uninit(&gh);
293         }
294 out:
295         return error;
296 out_unlock:
297         unlock_page(page);
298         goto out;
299 }
300
301 /**
302  * gfs2_prepare_write - Prepare to write a page to a file
303  * @file: The file to write to
304  * @page: The page which is to be prepared for writing
305  * @from: From (byte range within page)
306  * @to: To (byte range within page)
307  *
308  * Returns: errno
309  */
310
311 static int gfs2_prepare_write(struct file *file, struct page *page,
312                               unsigned from, unsigned to)
313 {
314         struct gfs2_inode *ip = get_v2ip(page->mapping->host);
315         struct gfs2_sbd *sdp = ip->i_sbd;
316         unsigned int data_blocks, ind_blocks, rblocks;
317         int alloc_required;
318         int error = 0;
319         loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + from;
320         loff_t end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
321         struct gfs2_alloc *al;
322
323         atomic_inc(&sdp->sd_ops_address);
324
325         gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME, &ip->i_gh);
326         error = gfs2_glock_nq_m_atime(1, &ip->i_gh);
327         if (error)
328                 goto out_uninit;
329
330         gfs2_write_calc_reserv(ip, to - from, &data_blocks, &ind_blocks);
331
332         error = gfs2_write_alloc_required(ip, pos, from - to, &alloc_required);
333         if (error)
334                 goto out_unlock;
335
336
337         if (alloc_required) {
338                 al = gfs2_alloc_get(ip);
339
340                 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
341                 if (error)
342                         goto out_alloc_put;
343
344                 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
345                 if (error)
346                         goto out_qunlock;
347
348                 al->al_requested = data_blocks + ind_blocks;
349                 error = gfs2_inplace_reserve(ip);
350                 if (error)
351                         goto out_qunlock;
352         }
353
354         rblocks = RES_DINODE + ind_blocks;
355         if (gfs2_is_jdata(ip))
356                 rblocks += data_blocks ? data_blocks : 1;
357         if (ind_blocks || data_blocks)
358                 rblocks += RES_STATFS + RES_QUOTA;
359
360         error = gfs2_trans_begin(sdp, rblocks, 0);
361         if (error)
362                 goto out;
363
364         if (gfs2_is_stuffed(ip)) {
365                 if (end > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
366                         error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page, page);
367                         if (error)
368                                 goto out;
369                 } else if (!PageUptodate(page)) {
370                         error = stuffed_readpage(ip, page);
371                         goto out;
372                 }
373         }
374
375         error = block_prepare_write(page, from, to, gfs2_get_block);
376
377 out:
378         if (error) {
379                 gfs2_trans_end(sdp);
380                 if (alloc_required) {
381                         gfs2_inplace_release(ip);
382 out_qunlock:
383                         gfs2_quota_unlock(ip);
384 out_alloc_put:
385                         gfs2_alloc_put(ip);
386                 }
387 out_unlock:
388                 gfs2_glock_dq_m(1, &ip->i_gh);
389 out_uninit:
390                 gfs2_holder_uninit(&ip->i_gh);
391         }
392
393         return error;
394 }
395
396 /**
397  * gfs2_commit_write - Commit write to a file
398  * @file: The file to write to
399  * @page: The page containing the data
400  * @from: From (byte range within page)
401  * @to: To (byte range within page)
402  *
403  * Returns: errno
404  */
405
406 static int gfs2_commit_write(struct file *file, struct page *page,
407                              unsigned from, unsigned to)
408 {
409         struct inode *inode = page->mapping->host;
410         struct gfs2_inode *ip = get_v2ip(inode);
411         struct gfs2_sbd *sdp = ip->i_sbd;
412         int error = -EOPNOTSUPP;
413         struct buffer_head *dibh;
414         struct gfs2_alloc *al = &ip->i_alloc;;
415
416         atomic_inc(&sdp->sd_ops_address);
417
418
419         if (gfs2_assert_withdraw(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)))
420                 goto fail_nounlock;
421
422         error = gfs2_meta_inode_buffer(ip, &dibh);
423         if (error)
424                 goto fail_endtrans;
425
426         gfs2_trans_add_bh(ip->i_gl, dibh, 1);
427
428         if (gfs2_is_stuffed(ip)) {
429                 uint64_t file_size;
430                 void *kaddr;
431
432                 file_size = ((uint64_t)page->index << PAGE_CACHE_SHIFT) + to;
433
434                 kaddr = kmap_atomic(page, KM_USER0);
435                 memcpy(dibh->b_data + sizeof(struct gfs2_dinode) + from,
436                        (char *)kaddr + from, to - from);
437                 kunmap_atomic(page, KM_USER0);
438
439                 SetPageUptodate(page);
440
441                 if (inode->i_size < file_size)
442                         i_size_write(inode, file_size);
443         } else {
444                 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
445                         gfs2_page_add_databufs(ip, page, from, to);
446                 error = generic_commit_write(file, page, from, to);
447                 if (error)
448                         goto fail;
449         }
450
451         if (ip->i_di.di_size < inode->i_size)
452                 ip->i_di.di_size = inode->i_size;
453
454         gfs2_dinode_out(&ip->i_di, dibh->b_data);
455         brelse(dibh);
456         gfs2_trans_end(sdp);
457         if (al->al_requested) {
458                 gfs2_inplace_release(ip);
459                 gfs2_quota_unlock(ip);
460                 gfs2_alloc_put(ip);
461         }
462         gfs2_glock_dq_m(1, &ip->i_gh);
463         gfs2_holder_uninit(&ip->i_gh);
464         return 0;
465
466 fail:
467         brelse(dibh);
468 fail_endtrans:
469         gfs2_trans_end(sdp);
470         if (al->al_requested) {
471                 gfs2_inplace_release(ip);
472                 gfs2_quota_unlock(ip);
473                 gfs2_alloc_put(ip);
474         }
475         gfs2_glock_dq_m(1, &ip->i_gh);
476         gfs2_holder_uninit(&ip->i_gh);
477 fail_nounlock:
478         ClearPageUptodate(page);
479         return error;
480 }
481
482 /**
483  * gfs2_bmap - Block map function
484  * @mapping: Address space info
485  * @lblock: The block to map
486  *
487  * Returns: The disk address for the block or 0 on hole or error
488  */
489
490 static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
491 {
492         struct gfs2_inode *ip = get_v2ip(mapping->host);
493         struct gfs2_holder i_gh;
494         sector_t dblock = 0;
495         int error;
496
497         atomic_inc(&ip->i_sbd->sd_ops_address);
498
499         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
500         if (error)
501                 return 0;
502
503         if (!gfs2_is_stuffed(ip))
504                 dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
505
506         gfs2_glock_dq_uninit(&i_gh);
507
508         return dblock;
509 }
510
511 static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh)
512 {
513         struct gfs2_bufdata *bd;
514
515         gfs2_log_lock(sdp);
516         bd = get_v2bd(bh);
517         if (bd) {
518                 bd->bd_bh = NULL;
519                 set_v2bd(bh, NULL);
520                 gfs2_log_unlock(sdp);
521                 brelse(bh);
522         } else
523                 gfs2_log_unlock(sdp);
524
525         lock_buffer(bh);
526         clear_buffer_dirty(bh);
527         bh->b_bdev = NULL;
528         clear_buffer_mapped(bh);
529         clear_buffer_req(bh);
530         clear_buffer_new(bh);
531         clear_buffer_delay(bh);
532         unlock_buffer(bh);
533 }
534
535 static int gfs2_invalidatepage(struct page *page, unsigned long offset)
536 {
537         struct gfs2_sbd *sdp = get_v2sdp(page->mapping->host->i_sb);
538         struct buffer_head *head, *bh, *next;
539         unsigned int curr_off = 0;
540         int ret = 1;
541
542         BUG_ON(!PageLocked(page));
543         if (!page_has_buffers(page))
544                 return 1;
545
546         bh = head = page_buffers(page);
547         do {
548                 unsigned int next_off = curr_off + bh->b_size;
549                 next = bh->b_this_page;
550
551                 if (offset <= curr_off)
552                         discard_buffer(sdp, bh);
553
554                 curr_off = next_off;
555                 bh = next;
556         } while (bh != head);
557
558         if (!offset)
559                 ret = try_to_release_page(page, 0);
560
561         return ret;
562 }
563
564 static ssize_t gfs2_direct_IO_write(struct kiocb *iocb, const struct iovec *iov,
565                                     loff_t offset, unsigned long nr_segs)
566 {
567         struct file *file = iocb->ki_filp;
568         struct inode *inode = file->f_mapping->host;
569         struct gfs2_inode *ip = get_v2ip(inode);
570         struct gfs2_holder gh;
571         int rv;
572
573         /*
574          * Shared lock, even though its write, since we do no allocation
575          * on this path. All we need change is atime.
576          */
577         gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
578         rv = gfs2_glock_nq_m_atime(1, &gh);
579         if (rv)
580                 goto out;
581
582         /*
583          * Should we return an error here? I can't see that O_DIRECT for
584          * a journaled file makes any sense. For now we'll silently fall
585          * back to buffered I/O, likewise we do the same for stuffed
586          * files since they are (a) small and (b) unaligned.
587          */
588         if (gfs2_is_jdata(ip))
589                 goto out;
590
591         if (gfs2_is_stuffed(ip))
592                 goto out;
593
594         rv = __blockdev_direct_IO(WRITE, iocb, inode, inode->i_sb->s_bdev,
595                                   iov, offset, nr_segs, get_blocks_noalloc,
596                                   NULL, DIO_OWN_LOCKING);
597 out:
598         gfs2_glock_dq_m(1, &gh);
599         gfs2_holder_uninit(&gh);
600
601         return rv;
602 }
603
604 /**
605  * gfs2_direct_IO
606  *
607  * This is called with a shared lock already held for the read path.
608  * Currently, no locks are held when the write path is called.
609  */
610 static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
611                               const struct iovec *iov, loff_t offset,
612                               unsigned long nr_segs)
613 {
614         struct file *file = iocb->ki_filp;
615         struct inode *inode = file->f_mapping->host;
616         struct gfs2_inode *ip = get_v2ip(inode);
617         struct gfs2_sbd *sdp = ip->i_sbd;
618
619         atomic_inc(&sdp->sd_ops_address);
620
621         if (rw == WRITE)
622                 return gfs2_direct_IO_write(iocb, iov, offset, nr_segs);
623
624         if (gfs2_assert_warn(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)) ||
625             gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
626                 return -EINVAL;
627
628         return __blockdev_direct_IO(READ, iocb, inode, inode->i_sb->s_bdev, iov,
629                                     offset, nr_segs, get_blocks, NULL,
630                                     DIO_OWN_LOCKING);
631 }
632
633 struct address_space_operations gfs2_file_aops = {
634         .writepage = gfs2_writepage,
635         .readpage = gfs2_readpage,
636         .sync_page = block_sync_page,
637         .prepare_write = gfs2_prepare_write,
638         .commit_write = gfs2_commit_write,
639         .bmap = gfs2_bmap,
640         .invalidatepage = gfs2_invalidatepage,
641         .direct_IO = gfs2_direct_IO,
642 };
643