[GFS2] Update GFS2 for the recent pull from Linus
[safe/jmp/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 /**
100  * gfs2_writepage - Write complete page
101  * @page: Page to write
102  *
103  * Returns: errno
104  *
105  * Some of this is copied from block_write_full_page() although we still
106  * call it to do most of the work.
107  */
108
109 static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
110 {
111         struct inode *inode = page->mapping->host;
112         struct gfs2_inode *ip = page->mapping->host->u.generic_ip;
113         struct gfs2_sbd *sdp = ip->i_sbd;
114         loff_t i_size = i_size_read(inode);
115         pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
116         unsigned offset;
117         int error;
118         int done_trans = 0;
119
120         if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) {
121                 unlock_page(page);
122                 return -EIO;
123         }
124         if (current->journal_info)
125                 goto out_ignore;
126
127         /* Is the page fully outside i_size? (truncate in progress) */
128         offset = i_size & (PAGE_CACHE_SIZE-1);
129         if (page->index >= end_index+1 || !offset) {
130                 page->mapping->a_ops->invalidatepage(page, 0);
131                 unlock_page(page);
132                 return 0; /* don't care */
133         }
134
135         if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) {
136                 error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
137                 if (error)
138                         goto out_ignore;
139                 gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
140                 done_trans = 1;
141         }
142         error = block_write_full_page(page, get_block_noalloc, wbc);
143         if (done_trans)
144                 gfs2_trans_end(sdp);
145         gfs2_meta_cache_flush(ip);
146         return error;
147
148 out_ignore:
149         redirty_page_for_writepage(wbc, page);
150         unlock_page(page);
151         return 0;
152 }
153
154 /**
155  * stuffed_readpage - Fill in a Linux page with stuffed file data
156  * @ip: the inode
157  * @page: the page
158  *
159  * Returns: errno
160  */
161
162 static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
163 {
164         struct buffer_head *dibh;
165         void *kaddr;
166         int error;
167
168         error = gfs2_meta_inode_buffer(ip, &dibh);
169         if (error)
170                 return error;
171
172         kaddr = kmap_atomic(page, KM_USER0);
173         memcpy((char *)kaddr,
174                dibh->b_data + sizeof(struct gfs2_dinode),
175                ip->i_di.di_size);
176         memset((char *)kaddr + ip->i_di.di_size,
177                0,
178                PAGE_CACHE_SIZE - ip->i_di.di_size);
179         kunmap_atomic(page, KM_USER0);
180
181         brelse(dibh);
182
183         SetPageUptodate(page);
184
185         return 0;
186 }
187
188 static int zero_readpage(struct page *page)
189 {
190         void *kaddr;
191
192         kaddr = kmap_atomic(page, KM_USER0);
193         memset(kaddr, 0, PAGE_CACHE_SIZE);
194         kunmap_atomic(page, KM_USER0);
195
196         SetPageUptodate(page);
197         unlock_page(page);
198
199         return 0;
200 }
201
202 /**
203  * gfs2_readpage - readpage with locking
204  * @file: The file to read a page for. N.B. This may be NULL if we are
205  * reading an internal file.
206  * @page: The page to read
207  *
208  * Returns: errno
209  */
210
211 static int gfs2_readpage(struct file *file, struct page *page)
212 {
213         struct gfs2_inode *ip = page->mapping->host->u.generic_ip;
214         struct gfs2_sbd *sdp = ip->i_sbd;
215         struct gfs2_holder gh;
216         int error;
217
218         if (file != &gfs2_internal_file_sentinal) {
219                 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
220                 error = gfs2_glock_nq_m_atime(1, &gh);
221                 if (error)
222                         goto out_unlock;
223         }
224
225         if (gfs2_is_stuffed(ip)) {
226                 if (!page->index) {
227                         error = stuffed_readpage(ip, page);
228                         unlock_page(page);
229                 } else
230                         error = zero_readpage(page);
231         } else
232                 error = mpage_readpage(page, gfs2_get_block);
233
234         if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
235                 error = -EIO;
236
237         if (file != &gfs2_internal_file_sentinal) {
238                 gfs2_glock_dq_m(1, &gh);
239                 gfs2_holder_uninit(&gh);
240         }
241 out:
242         return error;
243 out_unlock:
244         unlock_page(page);
245         goto out;
246 }
247
248 /**
249  * gfs2_prepare_write - Prepare to write a page to a file
250  * @file: The file to write to
251  * @page: The page which is to be prepared for writing
252  * @from: From (byte range within page)
253  * @to: To (byte range within page)
254  *
255  * Returns: errno
256  */
257
258 static int gfs2_prepare_write(struct file *file, struct page *page,
259                               unsigned from, unsigned to)
260 {
261         struct gfs2_inode *ip = page->mapping->host->u.generic_ip;
262         struct gfs2_sbd *sdp = ip->i_sbd;
263         unsigned int data_blocks, ind_blocks, rblocks;
264         int alloc_required;
265         int error = 0;
266         loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + from;
267         loff_t end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
268         struct gfs2_alloc *al;
269
270         gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME, &ip->i_gh);
271         error = gfs2_glock_nq_m_atime(1, &ip->i_gh);
272         if (error)
273                 goto out_uninit;
274
275         gfs2_write_calc_reserv(ip, to - from, &data_blocks, &ind_blocks);
276
277         error = gfs2_write_alloc_required(ip, pos, from - to, &alloc_required);
278         if (error)
279                 goto out_unlock;
280
281
282         if (alloc_required) {
283                 al = gfs2_alloc_get(ip);
284
285                 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
286                 if (error)
287                         goto out_alloc_put;
288
289                 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
290                 if (error)
291                         goto out_qunlock;
292
293                 al->al_requested = data_blocks + ind_blocks;
294                 error = gfs2_inplace_reserve(ip);
295                 if (error)
296                         goto out_qunlock;
297         }
298
299         rblocks = RES_DINODE + ind_blocks;
300         if (gfs2_is_jdata(ip))
301                 rblocks += data_blocks ? data_blocks : 1;
302         if (ind_blocks || data_blocks)
303                 rblocks += RES_STATFS + RES_QUOTA;
304
305         error = gfs2_trans_begin(sdp, rblocks, 0);
306         if (error)
307                 goto out;
308
309         if (gfs2_is_stuffed(ip)) {
310                 if (end > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
311                         error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page,
312                                                     page);
313                         if (error == 0)
314                                 goto prepare_write;
315                 } else if (!PageUptodate(page))
316                         error = stuffed_readpage(ip, page);
317                 goto out;
318         }
319
320 prepare_write:
321         error = block_prepare_write(page, from, to, gfs2_get_block);
322
323 out:
324         if (error) {
325                 gfs2_trans_end(sdp);
326                 if (alloc_required) {
327                         gfs2_inplace_release(ip);
328 out_qunlock:
329                         gfs2_quota_unlock(ip);
330 out_alloc_put:
331                         gfs2_alloc_put(ip);
332                 }
333 out_unlock:
334                 gfs2_glock_dq_m(1, &ip->i_gh);
335 out_uninit:
336                 gfs2_holder_uninit(&ip->i_gh);
337         }
338
339         return error;
340 }
341
342 /**
343  * gfs2_commit_write - Commit write to a file
344  * @file: The file to write to
345  * @page: The page containing the data
346  * @from: From (byte range within page)
347  * @to: To (byte range within page)
348  *
349  * Returns: errno
350  */
351
352 static int gfs2_commit_write(struct file *file, struct page *page,
353                              unsigned from, unsigned to)
354 {
355         struct inode *inode = page->mapping->host;
356         struct gfs2_inode *ip = inode->u.generic_ip;
357         struct gfs2_sbd *sdp = ip->i_sbd;
358         int error = -EOPNOTSUPP;
359         struct buffer_head *dibh;
360         struct gfs2_alloc *al = &ip->i_alloc;;
361
362         if (gfs2_assert_withdraw(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)))
363                 goto fail_nounlock;
364
365         error = gfs2_meta_inode_buffer(ip, &dibh);
366         if (error)
367                 goto fail_endtrans;
368
369         gfs2_trans_add_bh(ip->i_gl, dibh, 1);
370
371         if (gfs2_is_stuffed(ip)) {
372                 uint64_t file_size;
373                 void *kaddr;
374
375                 file_size = ((uint64_t)page->index << PAGE_CACHE_SHIFT) + to;
376
377                 kaddr = kmap_atomic(page, KM_USER0);
378                 memcpy(dibh->b_data + sizeof(struct gfs2_dinode) + from,
379                        (char *)kaddr + from, to - from);
380                 kunmap_atomic(page, KM_USER0);
381
382                 SetPageUptodate(page);
383
384                 if (inode->i_size < file_size)
385                         i_size_write(inode, file_size);
386         } else {
387                 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED ||
388                     gfs2_is_jdata(ip))
389                         gfs2_page_add_databufs(ip, page, from, to);
390                 error = generic_commit_write(file, page, from, to);
391                 if (error)
392                         goto fail;
393         }
394
395         if (ip->i_di.di_size < inode->i_size)
396                 ip->i_di.di_size = inode->i_size;
397
398         gfs2_dinode_out(&ip->i_di, dibh->b_data);
399         brelse(dibh);
400         gfs2_trans_end(sdp);
401         if (al->al_requested) {
402                 gfs2_inplace_release(ip);
403                 gfs2_quota_unlock(ip);
404                 gfs2_alloc_put(ip);
405         }
406         gfs2_glock_dq_m(1, &ip->i_gh);
407         gfs2_holder_uninit(&ip->i_gh);
408         return 0;
409
410 fail:
411         brelse(dibh);
412 fail_endtrans:
413         gfs2_trans_end(sdp);
414         if (al->al_requested) {
415                 gfs2_inplace_release(ip);
416                 gfs2_quota_unlock(ip);
417                 gfs2_alloc_put(ip);
418         }
419         gfs2_glock_dq_m(1, &ip->i_gh);
420         gfs2_holder_uninit(&ip->i_gh);
421 fail_nounlock:
422         ClearPageUptodate(page);
423         return error;
424 }
425
426 /**
427  * gfs2_bmap - Block map function
428  * @mapping: Address space info
429  * @lblock: The block to map
430  *
431  * Returns: The disk address for the block or 0 on hole or error
432  */
433
434 static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
435 {
436         struct gfs2_inode *ip = mapping->host->u.generic_ip;
437         struct gfs2_holder i_gh;
438         sector_t dblock = 0;
439         int error;
440
441         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
442         if (error)
443                 return 0;
444
445         if (!gfs2_is_stuffed(ip))
446                 dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
447
448         gfs2_glock_dq_uninit(&i_gh);
449
450         return dblock;
451 }
452
453 static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh)
454 {
455         struct gfs2_bufdata *bd;
456
457         gfs2_log_lock(sdp);
458         bd = bh->b_private;
459         if (bd) {
460                 bd->bd_bh = NULL;
461                 bh->b_private = NULL;
462                 gfs2_log_unlock(sdp);
463                 brelse(bh);
464         } else
465                 gfs2_log_unlock(sdp);
466
467         lock_buffer(bh);
468         clear_buffer_dirty(bh);
469         bh->b_bdev = NULL;
470         clear_buffer_mapped(bh);
471         clear_buffer_req(bh);
472         clear_buffer_new(bh);
473         clear_buffer_delay(bh);
474         unlock_buffer(bh);
475 }
476
477 static void gfs2_invalidatepage(struct page *page, unsigned long offset)
478 {
479         struct gfs2_sbd *sdp = page->mapping->host->i_sb->s_fs_info;
480         struct buffer_head *head, *bh, *next;
481         unsigned int curr_off = 0;
482
483         BUG_ON(!PageLocked(page));
484         if (!page_has_buffers(page))
485                 return;
486
487         bh = head = page_buffers(page);
488         do {
489                 unsigned int next_off = curr_off + bh->b_size;
490                 next = bh->b_this_page;
491
492                 if (offset <= curr_off)
493                         discard_buffer(sdp, bh);
494
495                 curr_off = next_off;
496                 bh = next;
497         } while (bh != head);
498
499         if (!offset)
500                 try_to_release_page(page, 0);
501
502         return;
503 }
504
505 static ssize_t gfs2_direct_IO_write(struct kiocb *iocb, const struct iovec *iov,
506                                     loff_t offset, unsigned long nr_segs)
507 {
508         struct file *file = iocb->ki_filp;
509         struct inode *inode = file->f_mapping->host;
510         struct gfs2_inode *ip = inode->u.generic_ip;
511         struct gfs2_holder gh;
512         int rv;
513
514         /*
515          * Shared lock, even though its write, since we do no allocation
516          * on this path. All we need change is atime.
517          */
518         gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
519         rv = gfs2_glock_nq_m_atime(1, &gh);
520         if (rv)
521                 goto out;
522
523         /*
524          * Should we return an error here? I can't see that O_DIRECT for
525          * a journaled file makes any sense. For now we'll silently fall
526          * back to buffered I/O, likewise we do the same for stuffed
527          * files since they are (a) small and (b) unaligned.
528          */
529         if (gfs2_is_jdata(ip))
530                 goto out;
531
532         if (gfs2_is_stuffed(ip))
533                 goto out;
534
535         rv = __blockdev_direct_IO(WRITE, iocb, inode, inode->i_sb->s_bdev,
536                                   iov, offset, nr_segs, gfs2_get_block,
537                                   NULL, DIO_OWN_LOCKING);
538 out:
539         gfs2_glock_dq_m(1, &gh);
540         gfs2_holder_uninit(&gh);
541
542         return rv;
543 }
544
545 /**
546  * gfs2_direct_IO
547  *
548  * This is called with a shared lock already held for the read path.
549  * Currently, no locks are held when the write path is called.
550  */
551 static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
552                               const struct iovec *iov, loff_t offset,
553                               unsigned long nr_segs)
554 {
555         struct file *file = iocb->ki_filp;
556         struct inode *inode = file->f_mapping->host;
557         struct gfs2_inode *ip = inode->u.generic_ip;
558         struct gfs2_sbd *sdp = ip->i_sbd;
559
560         if (rw == WRITE)
561                 return gfs2_direct_IO_write(iocb, iov, offset, nr_segs);
562
563         if (gfs2_assert_warn(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)) ||
564             gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
565                 return -EINVAL;
566
567         return __blockdev_direct_IO(READ, iocb, inode, inode->i_sb->s_bdev, iov,
568                                     offset, nr_segs, gfs2_get_block, NULL,
569                                     DIO_OWN_LOCKING);
570 }
571
572 struct address_space_operations gfs2_file_aops = {
573         .writepage = gfs2_writepage,
574         .readpage = gfs2_readpage,
575         .sync_page = block_sync_page,
576         .prepare_write = gfs2_prepare_write,
577         .commit_write = gfs2_commit_write,
578         .bmap = gfs2_bmap,
579         .invalidatepage = gfs2_invalidatepage,
580         .direct_IO = gfs2_direct_IO,
581 };
582