[GFS2] Use mutices rather than semaphores
[pandora-kernel.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 <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         error = block_write_full_page(page, get_block_noalloc, wbc);
193         if (done_trans)
194                 gfs2_trans_end(sdp);
195         gfs2_meta_cache_flush(ip);
196         return error;
197
198 out_ignore:
199         redirty_page_for_writepage(wbc, page);
200         unlock_page(page);
201         return 0;
202 }
203
204 /**
205  * stuffed_readpage - Fill in a Linux page with stuffed file data
206  * @ip: the inode
207  * @page: the page
208  *
209  * Returns: errno
210  */
211
212 static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
213 {
214         struct buffer_head *dibh;
215         void *kaddr;
216         int error;
217
218         error = gfs2_meta_inode_buffer(ip, &dibh);
219         if (error)
220                 return error;
221
222         kaddr = kmap_atomic(page, KM_USER0);
223         memcpy((char *)kaddr,
224                dibh->b_data + sizeof(struct gfs2_dinode),
225                ip->i_di.di_size);
226         memset((char *)kaddr + ip->i_di.di_size,
227                0,
228                PAGE_CACHE_SIZE - ip->i_di.di_size);
229         kunmap_atomic(page, KM_USER0);
230
231         brelse(dibh);
232
233         SetPageUptodate(page);
234
235         return 0;
236 }
237
238 static int zero_readpage(struct page *page)
239 {
240         void *kaddr;
241
242         kaddr = kmap_atomic(page, KM_USER0);
243         memset(kaddr, 0, PAGE_CACHE_SIZE);
244         kunmap_atomic(page, KM_USER0);
245
246         SetPageUptodate(page);
247         unlock_page(page);
248
249         return 0;
250 }
251
252 /**
253  * gfs2_readpage - readpage with locking
254  * @file: The file to read a page for. N.B. This may be NULL if we are
255  * reading an internal file.
256  * @page: The page to read
257  *
258  * Returns: errno
259  */
260
261 static int gfs2_readpage(struct file *file, struct page *page)
262 {
263         struct gfs2_inode *ip = get_v2ip(page->mapping->host);
264         struct gfs2_sbd *sdp = ip->i_sbd;
265         struct gfs2_holder gh;
266         int error;
267
268         atomic_inc(&sdp->sd_ops_address);
269
270         if (file != &gfs2_internal_file_sentinal) {
271                 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
272                 error = gfs2_glock_nq_m_atime(1, &gh);
273                 if (error)
274                         goto out_unlock;
275         }
276
277         if (gfs2_is_stuffed(ip)) {
278                 if (!page->index) {
279                         error = stuffed_readpage(ip, page);
280                         unlock_page(page);
281                 } else
282                         error = zero_readpage(page);
283         } else
284                 error = mpage_readpage(page, gfs2_get_block);
285
286         if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
287                 error = -EIO;
288
289         if (file != &gfs2_internal_file_sentinal) {
290                 gfs2_glock_dq_m(1, &gh);
291                 gfs2_holder_uninit(&gh);
292         }
293 out:
294         return error;
295 out_unlock:
296         unlock_page(page);
297         goto out;
298 }
299
300 /**
301  * gfs2_prepare_write - Prepare to write a page to a file
302  * @file: The file to write to
303  * @page: The page which is to be prepared for writing
304  * @from: From (byte range within page)
305  * @to: To (byte range within page)
306  *
307  * Returns: errno
308  */
309
310 static int gfs2_prepare_write(struct file *file, struct page *page,
311                               unsigned from, unsigned to)
312 {
313         struct gfs2_inode *ip = get_v2ip(page->mapping->host);
314         struct gfs2_sbd *sdp = ip->i_sbd;
315         unsigned int data_blocks, ind_blocks, rblocks;
316         int alloc_required;
317         int error = 0;
318         loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + from;
319         loff_t end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
320         struct gfs2_alloc *al;
321
322         atomic_inc(&sdp->sd_ops_address);
323
324         gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME, &ip->i_gh);
325         error = gfs2_glock_nq_m_atime(1, &ip->i_gh);
326         if (error)
327                 goto out_uninit;
328
329         gfs2_write_calc_reserv(ip, to - from, &data_blocks, &ind_blocks);
330
331         error = gfs2_write_alloc_required(ip, pos, from - to, &alloc_required);
332         if (error)
333                 goto out_unlock;
334
335
336         if (alloc_required) {
337                 al = gfs2_alloc_get(ip);
338
339                 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
340                 if (error)
341                         goto out_alloc_put;
342
343                 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
344                 if (error)
345                         goto out_qunlock;
346
347                 al->al_requested = data_blocks + ind_blocks;
348                 error = gfs2_inplace_reserve(ip);
349                 if (error)
350                         goto out_qunlock;
351         }
352
353         rblocks = RES_DINODE + ind_blocks;
354         if (gfs2_is_jdata(ip))
355                 rblocks += data_blocks ? data_blocks : 1;
356         if (ind_blocks || data_blocks)
357                 rblocks += RES_STATFS + RES_QUOTA;
358
359         error = gfs2_trans_begin(sdp, rblocks, 0);
360         if (error)
361                 goto out;
362
363         if (gfs2_is_stuffed(ip)) {
364                 if (end > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
365                         error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page, 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 = get_v2ip(inode);
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         atomic_inc(&sdp->sd_ops_address);
416
417
418         if (gfs2_assert_withdraw(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)))
419                 goto fail_nounlock;
420
421         error = gfs2_meta_inode_buffer(ip, &dibh);
422         if (error)
423                 goto fail_endtrans;
424
425         gfs2_trans_add_bh(ip->i_gl, dibh, 1);
426
427         if (gfs2_is_stuffed(ip)) {
428                 uint64_t file_size;
429                 void *kaddr;
430
431                 file_size = ((uint64_t)page->index << PAGE_CACHE_SHIFT) + to;
432
433                 kaddr = kmap_atomic(page, KM_USER0);
434                 memcpy(dibh->b_data + sizeof(struct gfs2_dinode) + from,
435                        (char *)kaddr + from, to - from);
436                 kunmap_atomic(page, KM_USER0);
437
438                 SetPageUptodate(page);
439
440                 if (inode->i_size < file_size)
441                         i_size_write(inode, file_size);
442         } else {
443                 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
444                         gfs2_page_add_databufs(ip, page, from, to);
445                 error = generic_commit_write(file, page, from, to);
446                 if (error)
447                         goto fail;
448         }
449
450         if (ip->i_di.di_size < inode->i_size)
451                 ip->i_di.di_size = inode->i_size;
452
453         gfs2_dinode_out(&ip->i_di, dibh->b_data);
454         brelse(dibh);
455         gfs2_trans_end(sdp);
456         if (al->al_requested) {
457                 gfs2_inplace_release(ip);
458                 gfs2_quota_unlock(ip);
459                 gfs2_alloc_put(ip);
460         }
461         gfs2_glock_dq_m(1, &ip->i_gh);
462         gfs2_holder_uninit(&ip->i_gh);
463         return 0;
464
465 fail:
466         brelse(dibh);
467 fail_endtrans:
468         gfs2_trans_end(sdp);
469         if (al->al_requested) {
470                 gfs2_inplace_release(ip);
471                 gfs2_quota_unlock(ip);
472                 gfs2_alloc_put(ip);
473         }
474         gfs2_glock_dq_m(1, &ip->i_gh);
475         gfs2_holder_uninit(&ip->i_gh);
476 fail_nounlock:
477         ClearPageUptodate(page);
478         return error;
479 }
480
481 /**
482  * gfs2_bmap - Block map function
483  * @mapping: Address space info
484  * @lblock: The block to map
485  *
486  * Returns: The disk address for the block or 0 on hole or error
487  */
488
489 static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
490 {
491         struct gfs2_inode *ip = get_v2ip(mapping->host);
492         struct gfs2_holder i_gh;
493         sector_t dblock = 0;
494         int error;
495
496         atomic_inc(&ip->i_sbd->sd_ops_address);
497
498         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
499         if (error)
500                 return 0;
501
502         if (!gfs2_is_stuffed(ip))
503                 dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
504
505         gfs2_glock_dq_uninit(&i_gh);
506
507         return dblock;
508 }
509
510 static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh)
511 {
512         struct gfs2_bufdata *bd;
513
514         gfs2_log_lock(sdp);
515         bd = get_v2bd(bh);
516         if (bd) {
517                 bd->bd_bh = NULL;
518                 set_v2bd(bh, NULL);
519                 gfs2_log_unlock(sdp);
520                 brelse(bh);
521         } else
522                 gfs2_log_unlock(sdp);
523
524         lock_buffer(bh);
525         clear_buffer_dirty(bh);
526         bh->b_bdev = NULL;
527         clear_buffer_mapped(bh);
528         clear_buffer_req(bh);
529         clear_buffer_new(bh);
530         clear_buffer_delay(bh);
531         unlock_buffer(bh);
532 }
533
534 static int gfs2_invalidatepage(struct page *page, unsigned long offset)
535 {
536         struct gfs2_sbd *sdp = get_v2sdp(page->mapping->host->i_sb);
537         struct buffer_head *head, *bh, *next;
538         unsigned int curr_off = 0;
539         int ret = 1;
540
541         BUG_ON(!PageLocked(page));
542         if (!page_has_buffers(page))
543                 return 1;
544
545         bh = head = page_buffers(page);
546         do {
547                 unsigned int next_off = curr_off + bh->b_size;
548                 next = bh->b_this_page;
549
550                 if (offset <= curr_off)
551                         discard_buffer(sdp, bh);
552
553                 curr_off = next_off;
554                 bh = next;
555         } while (bh != head);
556
557         if (!offset)
558                 ret = try_to_release_page(page, 0);
559
560         return ret;
561 }
562
563 static ssize_t gfs2_direct_IO_write(struct kiocb *iocb, const struct iovec *iov,
564                                     loff_t offset, unsigned long nr_segs)
565 {
566         struct file *file = iocb->ki_filp;
567         struct inode *inode = file->f_mapping->host;
568         struct gfs2_inode *ip = get_v2ip(inode);
569         struct gfs2_holder gh;
570         int rv;
571
572         /*
573          * Shared lock, even though its write, since we do no allocation
574          * on this path. All we need change is atime.
575          */
576         gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
577         rv = gfs2_glock_nq_m_atime(1, &gh);
578         if (rv)
579                 goto out;
580
581         /*
582          * Should we return an error here? I can't see that O_DIRECT for
583          * a journaled file makes any sense. For now we'll silently fall
584          * back to buffered I/O, likewise we do the same for stuffed
585          * files since they are (a) small and (b) unaligned.
586          */
587         if (gfs2_is_jdata(ip))
588                 goto out;
589
590         if (gfs2_is_stuffed(ip))
591                 goto out;
592
593         rv = __blockdev_direct_IO(WRITE, iocb, inode, inode->i_sb->s_bdev,
594                                   iov, offset, nr_segs, get_blocks_noalloc,
595                                   NULL, DIO_OWN_LOCKING);
596 out:
597         gfs2_glock_dq_m(1, &gh);
598         gfs2_holder_uninit(&gh);
599
600         return rv;
601 }
602
603 /**
604  * gfs2_direct_IO
605  *
606  * This is called with a shared lock already held for the read path.
607  * Currently, no locks are held when the write path is called.
608  */
609 static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
610                               const struct iovec *iov, loff_t offset,
611                               unsigned long nr_segs)
612 {
613         struct file *file = iocb->ki_filp;
614         struct inode *inode = file->f_mapping->host;
615         struct gfs2_inode *ip = get_v2ip(inode);
616         struct gfs2_sbd *sdp = ip->i_sbd;
617
618         atomic_inc(&sdp->sd_ops_address);
619
620         if (rw == WRITE)
621                 return gfs2_direct_IO_write(iocb, iov, offset, nr_segs);
622
623         if (gfs2_assert_warn(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)) ||
624             gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
625                 return -EINVAL;
626
627         return __blockdev_direct_IO(READ, iocb, inode, inode->i_sb->s_bdev, iov,
628                                     offset, nr_segs, get_blocks, NULL,
629                                     DIO_OWN_LOCKING);
630 }
631
632 struct address_space_operations gfs2_file_aops = {
633         .writepage = gfs2_writepage,
634         .readpage = gfs2_readpage,
635         .sync_page = block_sync_page,
636         .prepare_write = gfs2_prepare_write,
637         .commit_write = gfs2_commit_write,
638         .bmap = gfs2_bmap,
639         .invalidatepage = gfs2_invalidatepage,
640         .direct_IO = gfs2_direct_IO,
641 };
642