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/slab.h>
11 #include <linux/spinlock.h>
12 #include <linux/completion.h>
13 #include <linux/buffer_head.h>
14 #include <linux/pagemap.h>
15 #include <linux/uio.h>
16 #include <linux/blkdev.h>
19 #include <linux/gfs2_ondisk.h>
20 #include <linux/ext2_fs.h>
21 #include <linux/crc32.h>
22 #include <linux/lm_interface.h>
23 #include <linux/writeback.h>
24 #include <asm/uaccess.h>
45 * Most fields left uninitialised to catch anybody who tries to
46 * use them. f_flags set to prevent file_accessed() from touching
47 * any other part of this. Its use is purely as a flag so that we
48 * know (in readpage()) whether or not do to locking.
50 struct file gfs2_internal_file_sentinel = {
51 .f_flags = O_NOATIME|O_RDONLY,
54 static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
55 unsigned long offset, unsigned long size)
58 unsigned long count = desc->count;
64 memcpy(desc->arg.data, kaddr + offset, size);
67 desc->count = count - size;
68 desc->written += size;
69 desc->arg.buf += size;
73 int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
74 char *buf, loff_t *pos, unsigned size)
76 struct inode *inode = &ip->i_inode;
77 read_descriptor_t desc;
82 do_generic_mapping_read(inode->i_mapping, ra_state,
83 &gfs2_internal_file_sentinel, pos, &desc,
85 return desc.written ? desc.written : desc.error;
89 * gfs2_llseek - seek to a location in a file
92 * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
94 * SEEK_END requires the glock for the file because it references the
97 * Returns: The new offset, or errno
100 static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
102 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
103 struct gfs2_holder i_gh;
107 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
110 error = remote_llseek(file, offset, origin);
111 gfs2_glock_dq_uninit(&i_gh);
114 error = remote_llseek(file, offset, origin);
120 * gfs2_readdir - Read directory entries from a directory
121 * @file: The directory to read from
122 * @dirent: Buffer for dirents
123 * @filldir: Function used to do the copying
128 static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
130 struct inode *dir = file->f_mapping->host;
131 struct gfs2_inode *dip = GFS2_I(dir);
132 struct gfs2_holder d_gh;
133 u64 offset = file->f_pos;
136 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
137 error = gfs2_glock_nq_atime(&d_gh);
139 gfs2_holder_uninit(&d_gh);
143 error = gfs2_dir_read(dir, &offset, dirent, filldir);
145 gfs2_glock_dq_uninit(&d_gh);
147 file->f_pos = offset;
154 * @table: A table of 32 u32 flags
155 * @val: a 32 bit value to convert
157 * This function can be used to convert between fsflags values and
158 * GFS2's own flags values.
160 * Returns: the converted flags
162 static u32 fsflags_cvt(const u32 *table, u32 val)
174 static const u32 fsflags_to_gfs2[32] = {
176 [4] = GFS2_DIF_IMMUTABLE,
177 [5] = GFS2_DIF_APPENDONLY,
178 [7] = GFS2_DIF_NOATIME,
179 [12] = GFS2_DIF_EXHASH,
180 [14] = GFS2_DIF_INHERIT_JDATA,
181 [20] = GFS2_DIF_INHERIT_DIRECTIO,
184 static const u32 gfs2_to_fsflags[32] = {
185 [gfs2fl_Sync] = FS_SYNC_FL,
186 [gfs2fl_Immutable] = FS_IMMUTABLE_FL,
187 [gfs2fl_AppendOnly] = FS_APPEND_FL,
188 [gfs2fl_NoAtime] = FS_NOATIME_FL,
189 [gfs2fl_ExHash] = FS_INDEX_FL,
190 [gfs2fl_InheritDirectio] = FS_DIRECTIO_FL,
191 [gfs2fl_InheritJdata] = FS_JOURNAL_DATA_FL,
194 static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
196 struct inode *inode = filp->f_path.dentry->d_inode;
197 struct gfs2_inode *ip = GFS2_I(inode);
198 struct gfs2_holder gh;
202 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
203 error = gfs2_glock_nq_atime(&gh);
207 fsflags = fsflags_cvt(gfs2_to_fsflags, ip->i_di.di_flags);
208 if (!S_ISDIR(inode->i_mode)) {
209 if (ip->i_di.di_flags & GFS2_DIF_JDATA)
210 fsflags |= FS_JOURNAL_DATA_FL;
211 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
212 fsflags |= FS_DIRECTIO_FL;
214 if (put_user(fsflags, ptr))
217 gfs2_glock_dq_m(1, &gh);
218 gfs2_holder_uninit(&gh);
222 void gfs2_set_inode_flags(struct inode *inode)
224 struct gfs2_inode *ip = GFS2_I(inode);
225 struct gfs2_dinode_host *di = &ip->i_di;
226 unsigned int flags = inode->i_flags;
228 flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
229 if (di->di_flags & GFS2_DIF_IMMUTABLE)
230 flags |= S_IMMUTABLE;
231 if (di->di_flags & GFS2_DIF_APPENDONLY)
233 if (di->di_flags & GFS2_DIF_NOATIME)
235 if (di->di_flags & GFS2_DIF_SYNC)
237 inode->i_flags = flags;
240 /* Flags that can be set by user space */
241 #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
243 GFS2_DIF_IMMUTABLE| \
244 GFS2_DIF_APPENDONLY| \
248 GFS2_DIF_INHERIT_DIRECTIO| \
249 GFS2_DIF_INHERIT_JDATA)
252 * gfs2_set_flags - set flags on an inode
254 * @flags: The flags to set
255 * @mask: Indicates which flags are valid
258 static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
260 struct inode *inode = filp->f_path.dentry->d_inode;
261 struct gfs2_inode *ip = GFS2_I(inode);
262 struct gfs2_sbd *sdp = GFS2_SB(inode);
263 struct buffer_head *bh;
264 struct gfs2_holder gh;
266 u32 new_flags, flags;
268 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
272 flags = ip->i_di.di_flags;
273 new_flags = (flags & ~mask) | (reqflags & mask);
274 if ((new_flags ^ flags) == 0)
278 if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
282 if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
284 if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
286 if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
287 !capable(CAP_LINUX_IMMUTABLE))
289 if (!IS_IMMUTABLE(inode)) {
290 error = permission(inode, MAY_WRITE, NULL);
295 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
298 error = gfs2_meta_inode_buffer(ip, &bh);
301 gfs2_trans_add_bh(ip->i_gl, bh, 1);
302 ip->i_di.di_flags = new_flags;
303 gfs2_dinode_out(ip, bh->b_data);
305 gfs2_set_inode_flags(inode);
309 gfs2_glock_dq_uninit(&gh);
313 static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
315 struct inode *inode = filp->f_path.dentry->d_inode;
316 u32 fsflags, gfsflags;
317 if (get_user(fsflags, ptr))
319 gfsflags = fsflags_cvt(fsflags_to_gfs2, fsflags);
320 if (!S_ISDIR(inode->i_mode)) {
321 if (gfsflags & GFS2_DIF_INHERIT_JDATA)
322 gfsflags ^= (GFS2_DIF_JDATA | GFS2_DIF_INHERIT_JDATA);
323 if (gfsflags & GFS2_DIF_INHERIT_DIRECTIO)
324 gfsflags ^= (GFS2_DIF_DIRECTIO | GFS2_DIF_INHERIT_DIRECTIO);
325 return do_gfs2_set_flags(filp, gfsflags, ~0);
327 return do_gfs2_set_flags(filp, gfsflags, ~GFS2_DIF_JDATA);
330 static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
333 case FS_IOC_GETFLAGS:
334 return gfs2_get_flags(filp, (u32 __user *)arg);
335 case FS_IOC_SETFLAGS:
336 return gfs2_set_flags(filp, (u32 __user *)arg);
344 * @file: The file to map
345 * @vma: The VMA which described the mapping
347 * Returns: 0 or error code
350 static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
352 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
353 struct gfs2_holder i_gh;
356 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
357 error = gfs2_glock_nq_atime(&i_gh);
359 gfs2_holder_uninit(&i_gh);
363 /* This is VM_MAYWRITE instead of VM_WRITE because a call
364 to mprotect() can turn on VM_WRITE later. */
366 if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
367 (VM_MAYSHARE | VM_MAYWRITE))
368 vma->vm_ops = &gfs2_vm_ops_sharewrite;
370 vma->vm_ops = &gfs2_vm_ops_private;
372 gfs2_glock_dq_uninit(&i_gh);
378 * gfs2_open - open a file
379 * @inode: the inode to open
380 * @file: the struct file for this opening
385 static int gfs2_open(struct inode *inode, struct file *file)
387 struct gfs2_inode *ip = GFS2_I(inode);
388 struct gfs2_holder i_gh;
389 struct gfs2_file *fp;
392 fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
396 mutex_init(&fp->f_fl_mutex);
398 gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
399 file->private_data = fp;
401 if (S_ISREG(ip->i_inode.i_mode)) {
402 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
407 if (!(file->f_flags & O_LARGEFILE) &&
408 ip->i_di.di_size > MAX_NON_LFS) {
413 /* Listen to the Direct I/O flag */
415 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
416 file->f_flags |= O_DIRECT;
418 gfs2_glock_dq_uninit(&i_gh);
424 gfs2_glock_dq_uninit(&i_gh);
426 file->private_data = NULL;
432 * gfs2_close - called to close a struct file
433 * @inode: the inode the struct file belongs to
434 * @file: the struct file being closed
439 static int gfs2_close(struct inode *inode, struct file *file)
441 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
442 struct gfs2_file *fp;
444 fp = file->private_data;
445 file->private_data = NULL;
447 if (gfs2_assert_warn(sdp, fp))
456 * gfs2_fsync - sync the dirty data for a file (across the cluster)
457 * @file: the file that points to the dentry (we ignore this)
458 * @dentry: the dentry that points to the inode to sync
460 * The VFS will flush "normal" data for us. We only need to worry
461 * about metadata here. For journaled data, we just do a log flush
462 * as we can't avoid it. Otherwise we can just bale out if datasync
463 * is set. For stuffed inodes we must flush the log in order to
464 * ensure that all data is on disk.
466 * The call to write_inode_now() is there to write back metadata and
467 * the inode itself. It does also try and write the data, but thats
468 * (hopefully) a no-op due to the VFS having already called filemap_fdatawrite()
474 static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
476 struct inode *inode = dentry->d_inode;
477 int sync_state = inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC);
480 if (gfs2_is_jdata(GFS2_I(inode))) {
481 gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
485 if (sync_state != 0) {
487 ret = write_inode_now(inode, 0);
489 if (gfs2_is_stuffed(GFS2_I(inode)))
490 gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl);
497 * gfs2_setlease - acquire/release a file lease
498 * @file: the file pointer
505 static int gfs2_setlease(struct file *file, long arg, struct file_lock **fl)
507 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
510 * We don't currently have a way to enforce a lease across the whole
511 * cluster; until we do, disable leases (by just returning -EINVAL),
512 * unless the administrator has requested purely local locking.
514 if (!sdp->sd_args.ar_localflocks)
516 return generic_setlease(file, arg, fl);
520 * gfs2_lock - acquire/release a posix lock on a file
521 * @file: the file pointer
522 * @cmd: either modify or retrieve lock state, possibly wait
523 * @fl: type and range of lock
528 static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
530 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
531 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
532 struct lm_lockname name =
533 { .ln_number = ip->i_no_addr,
534 .ln_type = LM_TYPE_PLOCK };
536 if (!(fl->fl_flags & FL_POSIX))
538 if (__mandatory_lock(&ip->i_inode))
541 if (sdp->sd_args.ar_localflocks) {
543 posix_test_lock(file, fl);
546 return posix_lock_file_wait(file, fl);
550 if (cmd == F_CANCELLK) {
553 fl->fl_type = F_UNLCK;
556 return gfs2_lm_plock_get(sdp, &name, file, fl);
557 else if (fl->fl_type == F_UNLCK)
558 return gfs2_lm_punlock(sdp, &name, file, fl);
560 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
563 static int do_flock(struct file *file, int cmd, struct file_lock *fl)
565 struct gfs2_file *fp = file->private_data;
566 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
567 struct gfs2_inode *ip = GFS2_I(file->f_path.dentry->d_inode);
568 struct gfs2_glock *gl;
573 state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
574 flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE
577 mutex_lock(&fp->f_fl_mutex);
581 if (fl_gh->gh_state == state)
583 flock_lock_file_wait(file,
584 &(struct file_lock){.fl_type = F_UNLCK});
585 gfs2_glock_dq_wait(fl_gh);
586 gfs2_holder_reinit(state, flags, fl_gh);
588 error = gfs2_glock_get(GFS2_SB(&ip->i_inode),
589 ip->i_no_addr, &gfs2_flock_glops,
593 gfs2_holder_init(gl, state, flags, fl_gh);
596 error = gfs2_glock_nq(fl_gh);
598 gfs2_holder_uninit(fl_gh);
599 if (error == GLR_TRYFAILED)
602 error = flock_lock_file_wait(file, fl);
603 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
607 mutex_unlock(&fp->f_fl_mutex);
611 static void do_unflock(struct file *file, struct file_lock *fl)
613 struct gfs2_file *fp = file->private_data;
614 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
616 mutex_lock(&fp->f_fl_mutex);
617 flock_lock_file_wait(file, fl);
619 gfs2_glock_dq_uninit(fl_gh);
620 mutex_unlock(&fp->f_fl_mutex);
624 * gfs2_flock - acquire/release a flock lock on a file
625 * @file: the file pointer
626 * @cmd: either modify or retrieve lock state, possibly wait
627 * @fl: type and range of lock
632 static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
634 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
635 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
637 if (!(fl->fl_flags & FL_FLOCK))
639 if (__mandatory_lock(&ip->i_inode))
642 if (sdp->sd_args.ar_localflocks)
643 return flock_lock_file_wait(file, fl);
645 if (fl->fl_type == F_UNLCK) {
646 do_unflock(file, fl);
649 return do_flock(file, cmd, fl);
653 const struct file_operations gfs2_file_fops = {
654 .llseek = gfs2_llseek,
655 .read = do_sync_read,
656 .aio_read = generic_file_aio_read,
657 .write = do_sync_write,
658 .aio_write = generic_file_aio_write,
659 .unlocked_ioctl = gfs2_ioctl,
662 .release = gfs2_close,
666 .splice_read = generic_file_splice_read,
667 .splice_write = generic_file_splice_write,
668 .setlease = gfs2_setlease,
671 const struct file_operations gfs2_dir_fops = {
672 .readdir = gfs2_readdir,
673 .unlocked_ioctl = gfs2_ioctl,
675 .release = gfs2_close,