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 <asm/uaccess.h>
45 /* For regular, non-NFS */
47 struct gfs2_sbd *fdr_sbd;
50 filldir_t fdr_filldir;
55 * Most fields left uninitialised to catch anybody who tries to
56 * use them. f_flags set to prevent file_accessed() from touching
57 * any other part of this. Its use is purely as a flag so that we
58 * know (in readpage()) whether or not do to locking.
60 struct file gfs2_internal_file_sentinel = {
61 .f_flags = O_NOATIME|O_RDONLY,
64 static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
65 unsigned long offset, unsigned long size)
68 unsigned long count = desc->count;
74 memcpy(desc->arg.data, kaddr + offset, size);
77 desc->count = count - size;
78 desc->written += size;
79 desc->arg.buf += size;
83 int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
84 char *buf, loff_t *pos, unsigned size)
86 struct inode *inode = &ip->i_inode;
87 read_descriptor_t desc;
92 do_generic_mapping_read(inode->i_mapping, ra_state,
93 &gfs2_internal_file_sentinel, pos, &desc,
95 return desc.written ? desc.written : desc.error;
99 * gfs2_llseek - seek to a location in a file
101 * @offset: the offset
102 * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
104 * SEEK_END requires the glock for the file because it references the
107 * Returns: The new offset, or errno
110 static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
112 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
113 struct gfs2_holder i_gh;
117 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
120 error = remote_llseek(file, offset, origin);
121 gfs2_glock_dq_uninit(&i_gh);
124 error = remote_llseek(file, offset, origin);
130 * filldir_func - Report a directory entry to the caller of gfs2_dir_read()
131 * @opaque: opaque data used by the function
132 * @name: the name of the directory entry
133 * @length: the length of the name
134 * @offset: the entry's offset in the directory
135 * @inum: the inode number the entry points to
136 * @type: the type of inode the entry points to
138 * Returns: 0 on success, 1 if buffer full
141 static int filldir_func(void *opaque, const char *name, unsigned int length,
142 u64 offset, struct gfs2_inum_host *inum,
145 struct filldir_reg *fdr = (struct filldir_reg *)opaque;
146 struct gfs2_sbd *sdp = fdr->fdr_sbd;
149 error = fdr->fdr_filldir(fdr->fdr_opaque, name, length, offset,
150 inum->no_addr, type);
154 if (fdr->fdr_prefetch && !(length == 1 && *name == '.')) {
155 gfs2_glock_prefetch_num(sdp, inum->no_addr, &gfs2_inode_glops,
156 LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
157 gfs2_glock_prefetch_num(sdp, inum->no_addr, &gfs2_iopen_glops,
158 LM_ST_SHARED, LM_FLAG_TRY);
165 * gfs2_readdir - Read directory entries from a directory
166 * @file: The directory to read from
167 * @dirent: Buffer for dirents
168 * @filldir: Function used to do the copying
173 static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
175 struct inode *dir = file->f_mapping->host;
176 struct gfs2_inode *dip = GFS2_I(dir);
177 struct filldir_reg fdr;
178 struct gfs2_holder d_gh;
179 u64 offset = file->f_pos;
182 fdr.fdr_sbd = GFS2_SB(dir);
183 fdr.fdr_prefetch = 1;
184 fdr.fdr_filldir = filldir;
185 fdr.fdr_opaque = dirent;
187 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
188 error = gfs2_glock_nq_atime(&d_gh);
190 gfs2_holder_uninit(&d_gh);
194 error = gfs2_dir_read(dir, &offset, &fdr, filldir_func);
196 gfs2_glock_dq_uninit(&d_gh);
198 file->f_pos = offset;
205 * @table: A table of 32 u32 flags
206 * @val: a 32 bit value to convert
208 * This function can be used to convert between fsflags values and
209 * GFS2's own flags values.
211 * Returns: the converted flags
213 static u32 fsflags_cvt(const u32 *table, u32 val)
225 static const u32 fsflags_to_gfs2[32] = {
227 [4] = GFS2_DIF_IMMUTABLE,
228 [5] = GFS2_DIF_APPENDONLY,
229 [7] = GFS2_DIF_NOATIME,
230 [12] = GFS2_DIF_EXHASH,
231 [14] = GFS2_DIF_JDATA,
232 [20] = GFS2_DIF_DIRECTIO,
235 static const u32 gfs2_to_fsflags[32] = {
236 [gfs2fl_Sync] = FS_SYNC_FL,
237 [gfs2fl_Immutable] = FS_IMMUTABLE_FL,
238 [gfs2fl_AppendOnly] = FS_APPEND_FL,
239 [gfs2fl_NoAtime] = FS_NOATIME_FL,
240 [gfs2fl_ExHash] = FS_INDEX_FL,
241 [gfs2fl_Jdata] = FS_JOURNAL_DATA_FL,
242 [gfs2fl_Directio] = FS_DIRECTIO_FL,
243 [gfs2fl_InheritDirectio] = FS_DIRECTIO_FL,
244 [gfs2fl_InheritJdata] = FS_JOURNAL_DATA_FL,
247 static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
249 struct inode *inode = filp->f_dentry->d_inode;
250 struct gfs2_inode *ip = GFS2_I(inode);
251 struct gfs2_holder gh;
255 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
256 error = gfs2_glock_nq_m_atime(1, &gh);
260 fsflags = fsflags_cvt(gfs2_to_fsflags, ip->i_di.di_flags);
261 if (put_user(fsflags, ptr))
264 gfs2_glock_dq_m(1, &gh);
265 gfs2_holder_uninit(&gh);
269 void gfs2_set_inode_flags(struct inode *inode)
271 struct gfs2_inode *ip = GFS2_I(inode);
272 struct gfs2_dinode_host *di = &ip->i_di;
273 unsigned int flags = inode->i_flags;
275 flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
276 if (di->di_flags & GFS2_DIF_IMMUTABLE)
277 flags |= S_IMMUTABLE;
278 if (di->di_flags & GFS2_DIF_APPENDONLY)
280 if (di->di_flags & GFS2_DIF_NOATIME)
282 if (di->di_flags & GFS2_DIF_SYNC)
284 inode->i_flags = flags;
287 /* Flags that can be set by user space */
288 #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
290 GFS2_DIF_IMMUTABLE| \
291 GFS2_DIF_APPENDONLY| \
295 GFS2_DIF_INHERIT_DIRECTIO| \
296 GFS2_DIF_INHERIT_JDATA)
299 * gfs2_set_flags - set flags on an inode
301 * @flags: The flags to set
302 * @mask: Indicates which flags are valid
305 static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
307 struct inode *inode = filp->f_dentry->d_inode;
308 struct gfs2_inode *ip = GFS2_I(inode);
309 struct gfs2_sbd *sdp = GFS2_SB(inode);
310 struct buffer_head *bh;
311 struct gfs2_holder gh;
313 u32 new_flags, flags;
315 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
319 flags = ip->i_di.di_flags;
320 new_flags = (flags & ~mask) | (reqflags & mask);
321 if ((new_flags ^ flags) == 0)
324 if (S_ISDIR(inode->i_mode)) {
325 if ((new_flags ^ flags) & GFS2_DIF_JDATA)
326 new_flags ^= (GFS2_DIF_JDATA|GFS2_DIF_INHERIT_JDATA);
327 if ((new_flags ^ flags) & GFS2_DIF_DIRECTIO)
328 new_flags ^= (GFS2_DIF_DIRECTIO|GFS2_DIF_INHERIT_DIRECTIO);
332 if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET)
336 if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
338 if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
340 if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
341 !capable(CAP_LINUX_IMMUTABLE))
343 if (!IS_IMMUTABLE(inode)) {
344 error = permission(inode, MAY_WRITE, NULL);
349 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
352 error = gfs2_meta_inode_buffer(ip, &bh);
355 gfs2_trans_add_bh(ip->i_gl, bh, 1);
356 ip->i_di.di_flags = new_flags;
357 gfs2_dinode_out(ip, bh->b_data);
359 gfs2_set_inode_flags(inode);
363 gfs2_glock_dq_uninit(&gh);
367 static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
369 u32 fsflags, gfsflags;
370 if (get_user(fsflags, ptr))
372 gfsflags = fsflags_cvt(fsflags_to_gfs2, fsflags);
373 return do_gfs2_set_flags(filp, gfsflags, ~0);
376 static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
379 case FS_IOC_GETFLAGS:
380 return gfs2_get_flags(filp, (u32 __user *)arg);
381 case FS_IOC_SETFLAGS:
382 return gfs2_set_flags(filp, (u32 __user *)arg);
390 * @file: The file to map
391 * @vma: The VMA which described the mapping
393 * Returns: 0 or error code
396 static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
398 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
399 struct gfs2_holder i_gh;
402 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
403 error = gfs2_glock_nq_atime(&i_gh);
405 gfs2_holder_uninit(&i_gh);
409 /* This is VM_MAYWRITE instead of VM_WRITE because a call
410 to mprotect() can turn on VM_WRITE later. */
412 if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
413 (VM_MAYSHARE | VM_MAYWRITE))
414 vma->vm_ops = &gfs2_vm_ops_sharewrite;
416 vma->vm_ops = &gfs2_vm_ops_private;
418 gfs2_glock_dq_uninit(&i_gh);
424 * gfs2_open - open a file
425 * @inode: the inode to open
426 * @file: the struct file for this opening
431 static int gfs2_open(struct inode *inode, struct file *file)
433 struct gfs2_inode *ip = GFS2_I(inode);
434 struct gfs2_holder i_gh;
435 struct gfs2_file *fp;
438 fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
442 mutex_init(&fp->f_fl_mutex);
444 gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
445 file->private_data = fp;
447 if (S_ISREG(ip->i_inode.i_mode)) {
448 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
453 if (!(file->f_flags & O_LARGEFILE) &&
454 ip->i_di.di_size > MAX_NON_LFS) {
459 /* Listen to the Direct I/O flag */
461 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
462 file->f_flags |= O_DIRECT;
464 gfs2_glock_dq_uninit(&i_gh);
470 gfs2_glock_dq_uninit(&i_gh);
472 file->private_data = NULL;
478 * gfs2_close - called to close a struct file
479 * @inode: the inode the struct file belongs to
480 * @file: the struct file being closed
485 static int gfs2_close(struct inode *inode, struct file *file)
487 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
488 struct gfs2_file *fp;
490 fp = file->private_data;
491 file->private_data = NULL;
493 if (gfs2_assert_warn(sdp, fp))
502 * gfs2_fsync - sync the dirty data for a file (across the cluster)
503 * @file: the file that points to the dentry (we ignore this)
504 * @dentry: the dentry that points to the inode to sync
509 static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
511 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
513 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
519 * gfs2_lock - acquire/release a posix lock on a file
520 * @file: the file pointer
521 * @cmd: either modify or retrieve lock state, possibly wait
522 * @fl: type and range of lock
527 static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
529 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
530 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
531 struct lm_lockname name =
532 { .ln_number = ip->i_num.no_addr,
533 .ln_type = LM_TYPE_PLOCK };
535 if (!(fl->fl_flags & FL_POSIX))
537 if ((ip->i_inode.i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
540 if (sdp->sd_args.ar_localflocks) {
542 struct file_lock tmp;
544 ret = posix_test_lock(file, fl, &tmp);
545 fl->fl_type = F_UNLCK;
547 memcpy(fl, &tmp, sizeof(struct file_lock));
550 return posix_lock_file_wait(file, fl);
555 return gfs2_lm_plock_get(sdp, &name, file, fl);
556 else if (fl->fl_type == F_UNLCK)
557 return gfs2_lm_punlock(sdp, &name, file, fl);
559 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
562 static int do_flock(struct file *file, int cmd, struct file_lock *fl)
564 struct gfs2_file *fp = file->private_data;
565 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
566 struct gfs2_inode *ip = GFS2_I(file->f_dentry->d_inode);
567 struct gfs2_glock *gl;
572 state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
573 flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
575 mutex_lock(&fp->f_fl_mutex);
579 if (fl_gh->gh_state == state)
582 flock_lock_file_wait(file,
583 &(struct file_lock){.fl_type = F_UNLCK});
584 gfs2_glock_dq_uninit(fl_gh);
586 error = gfs2_glock_get(GFS2_SB(&ip->i_inode),
587 ip->i_num.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 ((ip->i_inode.i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
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,
665 .sendfile = generic_file_sendfile,
667 .splice_read = generic_file_splice_read,
668 .splice_write = generic_file_splice_write,
671 const struct file_operations gfs2_dir_fops = {
672 .readdir = gfs2_readdir,
673 .unlocked_ioctl = gfs2_ioctl,
675 .release = gfs2_close,