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.
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>
19 #include <linux/smp_lock.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>
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)
60 unsigned long count = desc->count;
66 memcpy(desc->arg.data, kaddr + offset, size);
69 desc->count = count - size;
70 desc->written += size;
71 desc->arg.buf += 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;
84 do_generic_mapping_read(inode->i_mapping, ra_state,
85 &gfs2_internal_file_sentinel, pos, &desc,
87 return desc.written ? desc.written : desc.error;
91 * gfs2_llseek - seek to a location in a file
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
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;
109 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
112 error = remote_llseek(file, offset, origin);
113 gfs2_glock_dq_uninit(&i_gh);
116 error = remote_llseek(file, offset, origin);
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
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;
138 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
139 error = gfs2_glock_nq_atime(&d_gh);
141 gfs2_holder_uninit(&d_gh);
145 error = gfs2_dir_read(dir, &offset, dirent, filldir);
147 gfs2_glock_dq_uninit(&d_gh);
149 file->f_pos = offset;
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)
176 static const u32 fsflags_to_gfs2[32] = {
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;
206 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
207 error = gfs2_glock_nq_atime(&gh);
211 fsflags = fsflags_cvt(gfs2_to_fsflags, ip->i_di.di_flags);
212 if (put_user(fsflags, ptr))
215 gfs2_glock_dq_m(1, &gh);
216 gfs2_holder_uninit(&gh);
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)
231 if (di->di_flags & GFS2_DIF_NOATIME)
233 if (di->di_flags & GFS2_DIF_SYNC)
235 inode->i_flags = flags;
238 /* Flags that can be set by user space */
239 #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
241 GFS2_DIF_IMMUTABLE| \
242 GFS2_DIF_APPENDONLY| \
246 GFS2_DIF_INHERIT_DIRECTIO| \
247 GFS2_DIF_INHERIT_JDATA)
250 * gfs2_set_flags - set flags on an 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;
264 u32 new_flags, flags;
266 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
270 flags = ip->i_di.di_flags;
271 new_flags = (flags & ~mask) | (reqflags & mask);
272 if ((new_flags ^ flags) == 0)
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);
283 if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
287 if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
289 if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
291 if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
292 !capable(CAP_LINUX_IMMUTABLE))
294 if (!IS_IMMUTABLE(inode)) {
295 error = permission(inode, MAY_WRITE, NULL);
300 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
303 error = gfs2_meta_inode_buffer(ip, &bh);
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);
310 gfs2_set_inode_flags(inode);
314 gfs2_glock_dq_uninit(&gh);
318 static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
320 u32 fsflags, gfsflags;
321 if (get_user(fsflags, ptr))
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)
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);
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;
353 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
354 error = gfs2_glock_nq_atime(&i_gh);
356 gfs2_holder_uninit(&i_gh);
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;
367 vma->vm_ops = &gfs2_vm_ops_private;
369 gfs2_glock_dq_uninit(&i_gh);
375 * gfs2_open - open a file
376 * @inode: the inode to open
377 * @file: the struct file for this opening
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;
389 fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
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,
404 if (!(file->f_flags & O_LARGEFILE) &&
405 ip->i_di.di_size > MAX_NON_LFS) {
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);
421 gfs2_glock_dq_uninit(&i_gh);
423 file->private_data = NULL;
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
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))
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()
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);
477 if (gfs2_is_jdata(GFS2_I(inode))) {
478 gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
482 if (sync_state != 0) {
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);
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
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))
512 if ((ip->i_inode.i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
515 if (sdp->sd_args.ar_localflocks) {
517 struct file_lock tmp;
519 ret = posix_test_lock(file, fl, &tmp);
520 fl->fl_type = F_UNLCK;
522 memcpy(fl, &tmp, sizeof(struct file_lock));
525 return posix_lock_file_wait(file, fl);
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);
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;
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);
554 if (fl_gh->gh_state == state)
557 flock_lock_file_wait(file,
558 &(struct file_lock){.fl_type = F_UNLCK});
559 gfs2_glock_dq_uninit(fl_gh);
561 error = gfs2_glock_get(GFS2_SB(&ip->i_inode),
562 ip->i_num.no_addr, &gfs2_flock_glops,
568 gfs2_holder_init(gl, state, flags, fl_gh);
571 error = gfs2_glock_nq(fl_gh);
573 gfs2_holder_uninit(fl_gh);
574 if (error == GLR_TRYFAILED)
577 error = flock_lock_file_wait(file, fl);
578 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
582 mutex_unlock(&fp->f_fl_mutex);
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);
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
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))
614 if ((ip->i_inode.i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
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);
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,
637 .release = gfs2_close,
640 .sendfile = generic_file_sendfile,
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,
650 .release = gfs2_close,