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