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