Merge master.kernel.org:/pub/scm/linux/kernel/git/bart/ide-2.6
[pandora-kernel.git] / fs / gfs2 / ops_file.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2006 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 version 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/uio.h>
17 #include <linux/blkdev.h>
18 #include <linux/mm.h>
19 #include <linux/smp_lock.h>
20 #include <linux/fs.h>
21 #include <linux/gfs2_ondisk.h>
22 #include <linux/ext2_fs.h>
23 #include <linux/crc32.h>
24 #include <linux/lm_interface.h>
25 #include <linux/writeback.h>
26 #include <asm/uaccess.h>
27
28 #include "gfs2.h"
29 #include "incore.h"
30 #include "bmap.h"
31 #include "dir.h"
32 #include "glock.h"
33 #include "glops.h"
34 #include "inode.h"
35 #include "lm.h"
36 #include "log.h"
37 #include "meta_io.h"
38 #include "ops_file.h"
39 #include "ops_vm.h"
40 #include "quota.h"
41 #include "rgrp.h"
42 #include "trans.h"
43 #include "util.h"
44 #include "eaops.h"
45
46 /*
47  * Most fields left uninitialised to catch anybody who tries to
48  * use them. f_flags set to prevent file_accessed() from touching
49  * any other part of this. Its use is purely as a flag so that we
50  * know (in readpage()) whether or not do to locking.
51  */
52 struct file gfs2_internal_file_sentinel = {
53         .f_flags = O_NOATIME|O_RDONLY,
54 };
55
56 static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
57                            unsigned long offset, unsigned long size)
58 {
59         char *kaddr;
60         unsigned long count = desc->count;
61
62         if (size > count)
63                 size = count;
64
65         kaddr = kmap(page);
66         memcpy(desc->arg.data, kaddr + offset, size);
67         kunmap(page);
68
69         desc->count = count - size;
70         desc->written += size;
71         desc->arg.buf += size;
72         return size;
73 }
74
75 int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
76                        char *buf, loff_t *pos, unsigned size)
77 {
78         struct inode *inode = &ip->i_inode;
79         read_descriptor_t desc;
80         desc.written = 0;
81         desc.arg.data = buf;
82         desc.count = size;
83         desc.error = 0;
84         do_generic_mapping_read(inode->i_mapping, ra_state,
85                                 &gfs2_internal_file_sentinel, pos, &desc,
86                                 gfs2_read_actor);
87         return desc.written ? desc.written : desc.error;
88 }
89
90 /**
91  * gfs2_llseek - seek to a location in a file
92  * @file: the file
93  * @offset: the offset
94  * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
95  *
96  * SEEK_END requires the glock for the file because it references the
97  * file's size.
98  *
99  * Returns: The new offset, or errno
100  */
101
102 static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
103 {
104         struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
105         struct gfs2_holder i_gh;
106         loff_t error;
107
108         if (origin == 2) {
109                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
110                                            &i_gh);
111                 if (!error) {
112                         error = remote_llseek(file, offset, origin);
113                         gfs2_glock_dq_uninit(&i_gh);
114                 }
115         } else
116                 error = remote_llseek(file, offset, origin);
117
118         return error;
119 }
120
121 /**
122  * gfs2_readdir - Read directory entries from a directory
123  * @file: The directory to read from
124  * @dirent: Buffer for dirents
125  * @filldir: Function used to do the copying
126  *
127  * Returns: errno
128  */
129
130 static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
131 {
132         struct inode *dir = file->f_mapping->host;
133         struct gfs2_inode *dip = GFS2_I(dir);
134         struct gfs2_holder d_gh;
135         u64 offset = file->f_pos;
136         int error;
137
138         gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
139         error = gfs2_glock_nq_atime(&d_gh);
140         if (error) {
141                 gfs2_holder_uninit(&d_gh);
142                 return error;
143         }
144
145         error = gfs2_dir_read(dir, &offset, dirent, filldir);
146
147         gfs2_glock_dq_uninit(&d_gh);
148
149         file->f_pos = offset;
150
151         return error;
152 }
153
154 /**
155  * fsflags_cvt
156  * @table: A table of 32 u32 flags
157  * @val: a 32 bit value to convert
158  *
159  * This function can be used to convert between fsflags values and
160  * GFS2's own flags values.
161  *
162  * Returns: the converted flags
163  */
164 static u32 fsflags_cvt(const u32 *table, u32 val)
165 {
166         u32 res = 0;
167         while(val) {
168                 if (val & 1)
169                         res |= *table;
170                 table++;
171                 val >>= 1;
172         }
173         return res;
174 }
175
176 static const u32 fsflags_to_gfs2[32] = {
177         [3] = GFS2_DIF_SYNC,
178         [4] = GFS2_DIF_IMMUTABLE,
179         [5] = GFS2_DIF_APPENDONLY,
180         [7] = GFS2_DIF_NOATIME,
181         [12] = GFS2_DIF_EXHASH,
182         [14] = GFS2_DIF_JDATA,
183         [20] = GFS2_DIF_DIRECTIO,
184 };
185
186 static const u32 gfs2_to_fsflags[32] = {
187         [gfs2fl_Sync] = FS_SYNC_FL,
188         [gfs2fl_Immutable] = FS_IMMUTABLE_FL,
189         [gfs2fl_AppendOnly] = FS_APPEND_FL,
190         [gfs2fl_NoAtime] = FS_NOATIME_FL,
191         [gfs2fl_ExHash] = FS_INDEX_FL,
192         [gfs2fl_Jdata] = FS_JOURNAL_DATA_FL,
193         [gfs2fl_Directio] = FS_DIRECTIO_FL,
194         [gfs2fl_InheritDirectio] = FS_DIRECTIO_FL,
195         [gfs2fl_InheritJdata] = FS_JOURNAL_DATA_FL,
196 };
197
198 static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
199 {
200         struct inode *inode = filp->f_path.dentry->d_inode;
201         struct gfs2_inode *ip = GFS2_I(inode);
202         struct gfs2_holder gh;
203         int error;
204         u32 fsflags;
205
206         gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
207         error = gfs2_glock_nq_atime(&gh);
208         if (error)
209                 return error;
210
211         fsflags = fsflags_cvt(gfs2_to_fsflags, ip->i_di.di_flags);
212         if (put_user(fsflags, ptr))
213                 error = -EFAULT;
214
215         gfs2_glock_dq_m(1, &gh);
216         gfs2_holder_uninit(&gh);
217         return error;
218 }
219
220 void gfs2_set_inode_flags(struct inode *inode)
221 {
222         struct gfs2_inode *ip = GFS2_I(inode);
223         struct gfs2_dinode_host *di = &ip->i_di;
224         unsigned int flags = inode->i_flags;
225
226         flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
227         if (di->di_flags & GFS2_DIF_IMMUTABLE)
228                 flags |= S_IMMUTABLE;
229         if (di->di_flags & GFS2_DIF_APPENDONLY)
230                 flags |= S_APPEND;
231         if (di->di_flags & GFS2_DIF_NOATIME)
232                 flags |= S_NOATIME;
233         if (di->di_flags & GFS2_DIF_SYNC)
234                 flags |= S_SYNC;
235         inode->i_flags = flags;
236 }
237
238 /* Flags that can be set by user space */
239 #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA|                    \
240                              GFS2_DIF_DIRECTIO|                 \
241                              GFS2_DIF_IMMUTABLE|                \
242                              GFS2_DIF_APPENDONLY|               \
243                              GFS2_DIF_NOATIME|                  \
244                              GFS2_DIF_SYNC|                     \
245                              GFS2_DIF_SYSTEM|                   \
246                              GFS2_DIF_INHERIT_DIRECTIO|         \
247                              GFS2_DIF_INHERIT_JDATA)
248
249 /**
250  * gfs2_set_flags - set flags on an inode
251  * @inode: The inode
252  * @flags: The flags to set
253  * @mask: Indicates which flags are valid
254  *
255  */
256 static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
257 {
258         struct inode *inode = filp->f_path.dentry->d_inode;
259         struct gfs2_inode *ip = GFS2_I(inode);
260         struct gfs2_sbd *sdp = GFS2_SB(inode);
261         struct buffer_head *bh;
262         struct gfs2_holder gh;
263         int error;
264         u32 new_flags, flags;
265
266         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
267         if (error)
268                 return error;
269
270         flags = ip->i_di.di_flags;
271         new_flags = (flags & ~mask) | (reqflags & mask);
272         if ((new_flags ^ flags) == 0)
273                 goto out;
274
275         if (S_ISDIR(inode->i_mode)) {
276                 if ((new_flags ^ flags) & GFS2_DIF_JDATA)
277                         new_flags ^= (GFS2_DIF_JDATA|GFS2_DIF_INHERIT_JDATA);
278                 if ((new_flags ^ flags) & GFS2_DIF_DIRECTIO)
279                         new_flags ^= (GFS2_DIF_DIRECTIO|GFS2_DIF_INHERIT_DIRECTIO);
280         }
281
282         error = -EINVAL;
283         if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
284                 goto out;
285
286         error = -EPERM;
287         if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
288                 goto out;
289         if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
290                 goto out;
291         if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
292             !capable(CAP_LINUX_IMMUTABLE))
293                 goto out;
294         if (!IS_IMMUTABLE(inode)) {
295                 error = permission(inode, MAY_WRITE, NULL);
296                 if (error)
297                         goto out;
298         }
299
300         error = gfs2_trans_begin(sdp, RES_DINODE, 0);
301         if (error)
302                 goto out;
303         error = gfs2_meta_inode_buffer(ip, &bh);
304         if (error)
305                 goto out_trans_end;
306         gfs2_trans_add_bh(ip->i_gl, bh, 1);
307         ip->i_di.di_flags = new_flags;
308         gfs2_dinode_out(ip, bh->b_data);
309         brelse(bh);
310         gfs2_set_inode_flags(inode);
311 out_trans_end:
312         gfs2_trans_end(sdp);
313 out:
314         gfs2_glock_dq_uninit(&gh);
315         return error;
316 }
317
318 static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
319 {
320         u32 fsflags, gfsflags;
321         if (get_user(fsflags, ptr))
322                 return -EFAULT;
323         gfsflags = fsflags_cvt(fsflags_to_gfs2, fsflags);
324         return do_gfs2_set_flags(filp, gfsflags, ~0);
325 }
326
327 static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
328 {
329         switch(cmd) {
330         case FS_IOC_GETFLAGS:
331                 return gfs2_get_flags(filp, (u32 __user *)arg);
332         case FS_IOC_SETFLAGS:
333                 return gfs2_set_flags(filp, (u32 __user *)arg);
334         }
335         return -ENOTTY;
336 }
337
338
339 /**
340  * gfs2_mmap -
341  * @file: The file to map
342  * @vma: The VMA which described the mapping
343  *
344  * Returns: 0 or error code
345  */
346
347 static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
348 {
349         struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
350         struct gfs2_holder i_gh;
351         int error;
352
353         gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
354         error = gfs2_glock_nq_atime(&i_gh);
355         if (error) {
356                 gfs2_holder_uninit(&i_gh);
357                 return error;
358         }
359
360         /* This is VM_MAYWRITE instead of VM_WRITE because a call
361            to mprotect() can turn on VM_WRITE later. */
362
363         if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
364             (VM_MAYSHARE | VM_MAYWRITE))
365                 vma->vm_ops = &gfs2_vm_ops_sharewrite;
366         else
367                 vma->vm_ops = &gfs2_vm_ops_private;
368
369         gfs2_glock_dq_uninit(&i_gh);
370
371         return error;
372 }
373
374 /**
375  * gfs2_open - open a file
376  * @inode: the inode to open
377  * @file: the struct file for this opening
378  *
379  * Returns: errno
380  */
381
382 static int gfs2_open(struct inode *inode, struct file *file)
383 {
384         struct gfs2_inode *ip = GFS2_I(inode);
385         struct gfs2_holder i_gh;
386         struct gfs2_file *fp;
387         int error;
388
389         fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
390         if (!fp)
391                 return -ENOMEM;
392
393         mutex_init(&fp->f_fl_mutex);
394
395         gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
396         file->private_data = fp;
397
398         if (S_ISREG(ip->i_inode.i_mode)) {
399                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
400                                            &i_gh);
401                 if (error)
402                         goto fail;
403
404                 if (!(file->f_flags & O_LARGEFILE) &&
405                     ip->i_di.di_size > MAX_NON_LFS) {
406                         error = -EFBIG;
407                         goto fail_gunlock;
408                 }
409
410                 /* Listen to the Direct I/O flag */
411
412                 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
413                         file->f_flags |= O_DIRECT;
414
415                 gfs2_glock_dq_uninit(&i_gh);
416         }
417
418         return 0;
419
420 fail_gunlock:
421         gfs2_glock_dq_uninit(&i_gh);
422 fail:
423         file->private_data = NULL;
424         kfree(fp);
425         return error;
426 }
427
428 /**
429  * gfs2_close - called to close a struct file
430  * @inode: the inode the struct file belongs to
431  * @file: the struct file being closed
432  *
433  * Returns: errno
434  */
435
436 static int gfs2_close(struct inode *inode, struct file *file)
437 {
438         struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
439         struct gfs2_file *fp;
440
441         fp = file->private_data;
442         file->private_data = NULL;
443
444         if (gfs2_assert_warn(sdp, fp))
445                 return -EIO;
446
447         kfree(fp);
448
449         return 0;
450 }
451
452 /**
453  * gfs2_fsync - sync the dirty data for a file (across the cluster)
454  * @file: the file that points to the dentry (we ignore this)
455  * @dentry: the dentry that points to the inode to sync
456  *
457  * The VFS will flush "normal" data for us. We only need to worry
458  * about metadata here. For journaled data, we just do a log flush
459  * as we can't avoid it. Otherwise we can just bale out if datasync
460  * is set. For stuffed inodes we must flush the log in order to
461  * ensure that all data is on disk.
462  *
463  * The call to write_inode_now() is there to write back metadata and
464  * the inode itself. It does also try and write the data, but thats
465  * (hopefully) a no-op due to the VFS having already called filemap_fdatawrite()
466  * for us.
467  *
468  * Returns: errno
469  */
470
471 static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
472 {
473         struct inode *inode = dentry->d_inode;
474         int sync_state = inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC);
475         int ret = 0;
476
477         if (gfs2_is_jdata(GFS2_I(inode))) {
478                 gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
479                 return 0;
480         }
481
482         if (sync_state != 0) {
483                 if (!datasync)
484                         ret = write_inode_now(inode, 0);
485
486                 if (gfs2_is_stuffed(GFS2_I(inode)))
487                         gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
488         }
489
490         return ret;
491 }
492
493 /**
494  * gfs2_lock - acquire/release a posix lock on a file
495  * @file: the file pointer
496  * @cmd: either modify or retrieve lock state, possibly wait
497  * @fl: type and range of lock
498  *
499  * Returns: errno
500  */
501
502 static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
503 {
504         struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
505         struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
506         struct lm_lockname name =
507                 { .ln_number = ip->i_num.no_addr,
508                   .ln_type = LM_TYPE_PLOCK };
509
510         if (!(fl->fl_flags & FL_POSIX))
511                 return -ENOLCK;
512         if ((ip->i_inode.i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
513                 return -ENOLCK;
514
515         if (sdp->sd_args.ar_localflocks) {
516                 if (IS_GETLK(cmd)) {
517                         struct file_lock tmp;
518                         int ret;
519                         ret = posix_test_lock(file, fl, &tmp);
520                         fl->fl_type = F_UNLCK;
521                         if (ret)
522                                 memcpy(fl, &tmp, sizeof(struct file_lock));
523                         return 0;
524                 } else {
525                         return posix_lock_file_wait(file, fl);
526                 }
527         }
528
529         if (IS_GETLK(cmd))
530                 return gfs2_lm_plock_get(sdp, &name, file, fl);
531         else if (fl->fl_type == F_UNLCK)
532                 return gfs2_lm_punlock(sdp, &name, file, fl);
533         else
534                 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
535 }
536
537 static int do_flock(struct file *file, int cmd, struct file_lock *fl)
538 {
539         struct gfs2_file *fp = file->private_data;
540         struct gfs2_holder *fl_gh = &fp->f_fl_gh;
541         struct gfs2_inode *ip = GFS2_I(file->f_path.dentry->d_inode);
542         struct gfs2_glock *gl;
543         unsigned int state;
544         int flags;
545         int error = 0;
546
547         state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
548         flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
549
550         mutex_lock(&fp->f_fl_mutex);
551
552         gl = fl_gh->gh_gl;
553         if (gl) {
554                 if (fl_gh->gh_state == state)
555                         goto out;
556                 gfs2_glock_hold(gl);
557                 flock_lock_file_wait(file,
558                                      &(struct file_lock){.fl_type = F_UNLCK});
559                 gfs2_glock_dq_uninit(fl_gh);
560         } else {
561                 error = gfs2_glock_get(GFS2_SB(&ip->i_inode),
562                                       ip->i_num.no_addr, &gfs2_flock_glops,
563                                       CREATE, &gl);
564                 if (error)
565                         goto out;
566         }
567
568         gfs2_holder_init(gl, state, flags, fl_gh);
569         gfs2_glock_put(gl);
570
571         error = gfs2_glock_nq(fl_gh);
572         if (error) {
573                 gfs2_holder_uninit(fl_gh);
574                 if (error == GLR_TRYFAILED)
575                         error = -EAGAIN;
576         } else {
577                 error = flock_lock_file_wait(file, fl);
578                 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
579         }
580
581 out:
582         mutex_unlock(&fp->f_fl_mutex);
583         return error;
584 }
585
586 static void do_unflock(struct file *file, struct file_lock *fl)
587 {
588         struct gfs2_file *fp = file->private_data;
589         struct gfs2_holder *fl_gh = &fp->f_fl_gh;
590
591         mutex_lock(&fp->f_fl_mutex);
592         flock_lock_file_wait(file, fl);
593         if (fl_gh->gh_gl)
594                 gfs2_glock_dq_uninit(fl_gh);
595         mutex_unlock(&fp->f_fl_mutex);
596 }
597
598 /**
599  * gfs2_flock - acquire/release a flock lock on a file
600  * @file: the file pointer
601  * @cmd: either modify or retrieve lock state, possibly wait
602  * @fl: type and range of lock
603  *
604  * Returns: errno
605  */
606
607 static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
608 {
609         struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
610         struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
611
612         if (!(fl->fl_flags & FL_FLOCK))
613                 return -ENOLCK;
614         if ((ip->i_inode.i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
615                 return -ENOLCK;
616
617         if (sdp->sd_args.ar_localflocks)
618                 return flock_lock_file_wait(file, fl);
619
620         if (fl->fl_type == F_UNLCK) {
621                 do_unflock(file, fl);
622                 return 0;
623         } else {
624                 return do_flock(file, cmd, fl);
625         }
626 }
627
628 const struct file_operations gfs2_file_fops = {
629         .llseek         = gfs2_llseek,
630         .read           = do_sync_read,
631         .aio_read       = generic_file_aio_read,
632         .write          = do_sync_write,
633         .aio_write      = generic_file_aio_write,
634         .unlocked_ioctl = gfs2_ioctl,
635         .mmap           = gfs2_mmap,
636         .open           = gfs2_open,
637         .release        = gfs2_close,
638         .fsync          = gfs2_fsync,
639         .lock           = gfs2_lock,
640         .sendfile       = generic_file_sendfile,
641         .flock          = gfs2_flock,
642         .splice_read    = generic_file_splice_read,
643         .splice_write   = generic_file_splice_write,
644 };
645
646 const struct file_operations gfs2_dir_fops = {
647         .readdir        = gfs2_readdir,
648         .unlocked_ioctl = gfs2_ioctl,
649         .open           = gfs2_open,
650         .release        = gfs2_close,
651         .fsync          = gfs2_fsync,
652         .lock           = gfs2_lock,
653         .flock          = gfs2_flock,
654 };
655