[ALSA] echo3g_dsp.c shouldn't include #include <linux/irq.h>
[linux-2.6/cjktty.git] / fs / gfs2 / ops_file.c
blobc996aa739a0515f5c11229591a83805a4abb6f99
1 /*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
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 */
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>
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"
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.
52 struct file gfs2_internal_file_sentinel = {
53 .f_flags = O_NOATIME|O_RDONLY,
56 static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
57 unsigned long offset, unsigned long size)
59 char *kaddr;
60 unsigned long count = desc->count;
62 if (size > count)
63 size = count;
65 kaddr = kmap(page);
66 memcpy(desc->arg.data, kaddr + offset, size);
67 kunmap(page);
69 desc->count = count - size;
70 desc->written += size;
71 desc->arg.buf += size;
72 return size;
75 int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
76 char *buf, loff_t *pos, unsigned size)
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;
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)
96 * SEEK_END requires the glock for the file because it references the
97 * file's size.
99 * Returns: The new offset, or errno
102 static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
104 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
105 struct gfs2_holder i_gh;
106 loff_t error;
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);
115 } else
116 error = remote_llseek(file, offset, origin);
118 return error;
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
127 * Returns: errno
130 static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
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;
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;
145 error = gfs2_dir_read(dir, &offset, dirent, filldir);
147 gfs2_glock_dq_uninit(&d_gh);
149 file->f_pos = offset;
151 return error;
155 * fsflags_cvt
156 * @table: A table of 32 u32 flags
157 * @val: a 32 bit value to convert
159 * This function can be used to convert between fsflags values and
160 * GFS2's own flags values.
162 * Returns: the converted flags
164 static u32 fsflags_cvt(const u32 *table, u32 val)
166 u32 res = 0;
167 while(val) {
168 if (val & 1)
169 res |= *table;
170 table++;
171 val >>= 1;
173 return res;
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,
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,
198 static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
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;
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;
211 fsflags = fsflags_cvt(gfs2_to_fsflags, ip->i_di.di_flags);
212 if (put_user(fsflags, ptr))
213 error = -EFAULT;
215 gfs2_glock_dq_m(1, &gh);
216 gfs2_holder_uninit(&gh);
217 return error;
220 void gfs2_set_inode_flags(struct inode *inode)
222 struct gfs2_inode *ip = GFS2_I(inode);
223 struct gfs2_dinode_host *di = &ip->i_di;
224 unsigned int flags = inode->i_flags;
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;
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)
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
256 static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
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;
266 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
267 if (error)
268 return error;
270 flags = ip->i_di.di_flags;
271 new_flags = (flags & ~mask) | (reqflags & mask);
272 if ((new_flags ^ flags) == 0)
273 goto out;
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);
282 error = -EINVAL;
283 if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
284 goto out;
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;
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;
318 static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
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);
327 static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
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);
335 return -ENOTTY;
340 * gfs2_mmap -
341 * @file: The file to map
342 * @vma: The VMA which described the mapping
344 * Returns: 0 or error code
347 static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
349 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
350 struct gfs2_holder i_gh;
351 int error;
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;
360 /* This is VM_MAYWRITE instead of VM_WRITE because a call
361 to mprotect() can turn on VM_WRITE later. */
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;
369 gfs2_glock_dq_uninit(&i_gh);
371 return error;
375 * gfs2_open - open a file
376 * @inode: the inode to open
377 * @file: the struct file for this opening
379 * Returns: errno
382 static int gfs2_open(struct inode *inode, struct file *file)
384 struct gfs2_inode *ip = GFS2_I(inode);
385 struct gfs2_holder i_gh;
386 struct gfs2_file *fp;
387 int error;
389 fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
390 if (!fp)
391 return -ENOMEM;
393 mutex_init(&fp->f_fl_mutex);
395 gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
396 file->private_data = fp;
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;
404 if (!(file->f_flags & O_LARGEFILE) &&
405 ip->i_di.di_size > MAX_NON_LFS) {
406 error = -EFBIG;
407 goto fail_gunlock;
410 /* Listen to the Direct I/O flag */
412 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
413 file->f_flags |= O_DIRECT;
415 gfs2_glock_dq_uninit(&i_gh);
418 return 0;
420 fail_gunlock:
421 gfs2_glock_dq_uninit(&i_gh);
422 fail:
423 file->private_data = NULL;
424 kfree(fp);
425 return error;
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
433 * Returns: errno
436 static int gfs2_close(struct inode *inode, struct file *file)
438 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
439 struct gfs2_file *fp;
441 fp = file->private_data;
442 file->private_data = NULL;
444 if (gfs2_assert_warn(sdp, fp))
445 return -EIO;
447 kfree(fp);
449 return 0;
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
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.
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.
468 * Returns: errno
471 static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
473 struct inode *inode = dentry->d_inode;
474 int sync_state = inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC);
475 int ret = 0;
477 if (gfs2_is_jdata(GFS2_I(inode))) {
478 gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
479 return 0;
482 if (sync_state != 0) {
483 if (!datasync)
484 ret = write_inode_now(inode, 0);
486 if (gfs2_is_stuffed(GFS2_I(inode)))
487 gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
490 return ret;
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
499 * Returns: errno
502 static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
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 };
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;
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);
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);
537 static int do_flock(struct file *file, int cmd, struct file_lock *fl)
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;
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;
550 mutex_lock(&fp->f_fl_mutex);
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;
568 gfs2_holder_init(gl, state, flags, fl_gh);
569 gfs2_glock_put(gl);
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);
581 out:
582 mutex_unlock(&fp->f_fl_mutex);
583 return error;
586 static void do_unflock(struct file *file, struct file_lock *fl)
588 struct gfs2_file *fp = file->private_data;
589 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
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);
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
604 * Returns: errno
607 static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
609 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
610 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
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;
617 if (sdp->sd_args.ar_localflocks)
618 return flock_lock_file_wait(file, fl);
620 if (fl->fl_type == F_UNLCK) {
621 do_unflock(file, fl);
622 return 0;
623 } else {
624 return do_flock(file, cmd, fl);
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,
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,