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