[GFS2] 80 Column audit of GFS2
[safe/jmp/linux-2.6] / fs / gfs2 / ops_file.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2005 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 v.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/gfs2_ioctl.h>
21 #include <linux/fs.h>
22 #include <asm/semaphore.h>
23 #include <asm/uaccess.h>
24
25 #include "gfs2.h"
26 #include "bmap.h"
27 #include "dir.h"
28 #include "glock.h"
29 #include "glops.h"
30 #include "inode.h"
31 #include "lm.h"
32 #include "log.h"
33 #include "meta_io.h"
34 #include "ops_file.h"
35 #include "ops_vm.h"
36 #include "quota.h"
37 #include "rgrp.h"
38 #include "trans.h"
39
40 /* "bad" is for NFS support */
41 struct filldir_bad_entry {
42         char *fbe_name;
43         unsigned int fbe_length;
44         uint64_t fbe_offset;
45         struct gfs2_inum fbe_inum;
46         unsigned int fbe_type;
47 };
48
49 struct filldir_bad {
50         struct gfs2_sbd *fdb_sbd;
51
52         struct filldir_bad_entry *fdb_entry;
53         unsigned int fdb_entry_num;
54         unsigned int fdb_entry_off;
55
56         char *fdb_name;
57         unsigned int fdb_name_size;
58         unsigned int fdb_name_off;
59 };
60
61 /* For regular, non-NFS */
62 struct filldir_reg {
63         struct gfs2_sbd *fdr_sbd;
64         int fdr_prefetch;
65
66         filldir_t fdr_filldir;
67         void *fdr_opaque;
68 };
69
70 /*
71  * Most fields left uninitialised to catch anybody who tries to
72  * use them. f_flags set to prevent file_accessed() from touching
73  * any other part of this. Its use is purely as a flag so that we
74  * know (in readpage()) whether or not do to locking.
75  */
76 struct file gfs2_internal_file_sentinal = {
77         .f_flags = O_NOATIME|O_RDONLY,
78 };
79
80 static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
81                            unsigned long offset, unsigned long size)
82 {
83         char *kaddr;
84         unsigned long count = desc->count;
85
86         if (size > count)
87                 size = count;
88
89         kaddr = kmap(page);
90         memcpy(desc->arg.buf, kaddr + offset, size);
91         kunmap(page);
92
93         desc->count = count - size;
94         desc->written += size;
95         desc->arg.buf += size;
96         return size;
97 }
98
99 int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
100                        char *buf, loff_t *pos, unsigned size)
101 {
102         struct inode *inode = ip->i_vnode;
103         read_descriptor_t desc;
104         desc.written = 0;
105         desc.arg.buf = buf;
106         desc.count = size;
107         desc.error = 0;
108         do_generic_mapping_read(inode->i_mapping, ra_state,
109                                 &gfs2_internal_file_sentinal, pos, &desc,
110                                 gfs2_read_actor);
111         return desc.written ? desc.written : desc.error;
112 }
113
114 /**
115  * gfs2_llseek - seek to a location in a file
116  * @file: the file
117  * @offset: the offset
118  * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
119  *
120  * SEEK_END requires the glock for the file because it references the
121  * file's size.
122  *
123  * Returns: The new offset, or errno
124  */
125
126 static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
127 {
128         struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
129         struct gfs2_holder i_gh;
130         loff_t error;
131
132         if (origin == 2) {
133                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
134                                            &i_gh);
135                 if (!error) {
136                         error = remote_llseek(file, offset, origin);
137                         gfs2_glock_dq_uninit(&i_gh);
138                 }
139         } else
140                 error = remote_llseek(file, offset, origin);
141
142         return error;
143 }
144
145
146 static ssize_t gfs2_direct_IO_read(struct kiocb *iocb, const struct iovec *iov,
147                                    loff_t offset, unsigned long nr_segs)
148 {
149         struct file *file = iocb->ki_filp;
150         struct address_space *mapping = file->f_mapping;
151         ssize_t retval;
152
153         retval = filemap_write_and_wait(mapping);
154         if (retval == 0) {
155                 retval = mapping->a_ops->direct_IO(READ, iocb, iov, offset,
156                                                    nr_segs);
157         }
158         return retval;
159 }
160
161 /**
162  * __gfs2_file_aio_read - The main GFS2 read function
163  * 
164  * N.B. This is almost, but not quite the same as __generic_file_aio_read()
165  * the important subtle different being that inode->i_size isn't valid
166  * unless we are holding a lock, and we do this _only_ on the O_DIRECT
167  * path since otherwise locking is done entirely at the page cache
168  * layer.
169  */
170 static ssize_t __gfs2_file_aio_read(struct kiocb *iocb,
171                                     const struct iovec *iov,
172                                     unsigned long nr_segs, loff_t *ppos)
173 {
174         struct file *filp = iocb->ki_filp;
175         struct gfs2_inode *ip = get_v2ip(filp->f_mapping->host);
176         struct gfs2_holder gh;
177         ssize_t retval;
178         unsigned long seg;
179         size_t count;
180
181         count = 0;
182         for (seg = 0; seg < nr_segs; seg++) {
183                 const struct iovec *iv = &iov[seg];
184
185                 /*
186                  * If any segment has a negative length, or the cumulative
187                  * length ever wraps negative then return -EINVAL.
188                  */
189                 count += iv->iov_len;
190                 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
191                         return -EINVAL;
192                 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
193                         continue;
194                 if (seg == 0)
195                         return -EFAULT;
196                 nr_segs = seg;
197                 count -= iv->iov_len;   /* This segment is no good */
198                 break;
199         }
200
201         /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
202         if (filp->f_flags & O_DIRECT) {
203                 loff_t pos = *ppos, size;
204                 struct address_space *mapping;
205                 struct inode *inode;
206
207                 mapping = filp->f_mapping;
208                 inode = mapping->host;
209                 retval = 0;
210                 if (!count)
211                         goto out; /* skip atime */
212
213                 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
214                 retval = gfs2_glock_nq_m_atime(1, &gh);
215                 if (retval)
216                         goto out;
217                 if (gfs2_is_stuffed(ip)) {
218                         gfs2_glock_dq_m(1, &gh);
219                         gfs2_holder_uninit(&gh);
220                         goto fallback_to_normal;
221                 }
222                 size = i_size_read(inode);
223                 if (pos < size) {
224                         retval = gfs2_direct_IO_read(iocb, iov, pos, nr_segs);
225                         if (retval > 0 && !is_sync_kiocb(iocb))
226                                 retval = -EIOCBQUEUED;
227                         if (retval > 0)
228                                 *ppos = pos + retval;
229                 }
230                 file_accessed(filp);
231                 gfs2_glock_dq_m(1, &gh);
232                 gfs2_holder_uninit(&gh);
233                 goto out;
234         }
235
236 fallback_to_normal:
237         retval = 0;
238         if (count) {
239                 for (seg = 0; seg < nr_segs; seg++) {
240                         read_descriptor_t desc;
241
242                         desc.written = 0;
243                         desc.arg.buf = iov[seg].iov_base;
244                         desc.count = iov[seg].iov_len;
245                         if (desc.count == 0)
246                                 continue;
247                         desc.error = 0;
248                         do_generic_file_read(filp,ppos,&desc,file_read_actor);
249                         retval += desc.written;
250                         if (desc.error) {
251                                 retval = retval ?: desc.error;
252                                  break;
253                         }
254                 }
255         }
256 out:
257         return retval;
258 }
259
260 /**
261  * gfs2_read - Read bytes from a file
262  * @file: The file to read from
263  * @buf: The buffer to copy into
264  * @size: The amount of data requested
265  * @offset: The current file offset
266  *
267  * Outputs: Offset - updated according to number of bytes read
268  *
269  * Returns: The number of bytes read, errno on failure
270  */
271
272 static ssize_t gfs2_read(struct file *filp, char __user *buf, size_t size,
273                          loff_t *offset)
274 {
275         struct iovec local_iov = { .iov_base = buf, .iov_len = size };
276         struct kiocb kiocb;
277         ssize_t ret;
278
279         init_sync_kiocb(&kiocb, filp);
280         ret = __gfs2_file_aio_read(&kiocb, &local_iov, 1, offset);
281         if (-EIOCBQUEUED == ret)
282                 ret = wait_on_sync_kiocb(&kiocb);
283         return ret;
284 }
285
286 static ssize_t gfs2_file_readv(struct file *filp, const struct iovec *iov,
287                                unsigned long nr_segs, loff_t *ppos)
288 {
289         struct kiocb kiocb;
290         ssize_t ret;
291
292         init_sync_kiocb(&kiocb, filp);
293         ret = __gfs2_file_aio_read(&kiocb, iov, nr_segs, ppos);
294         if (-EIOCBQUEUED == ret)
295                 ret = wait_on_sync_kiocb(&kiocb);
296         return ret;
297 }
298
299 static ssize_t gfs2_file_aio_read(struct kiocb *iocb, char __user *buf,
300                                   size_t count, loff_t pos)
301 {
302         struct iovec local_iov = { .iov_base = buf, .iov_len = count };
303
304         BUG_ON(iocb->ki_pos != pos);
305         return __gfs2_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
306 }
307
308
309 /**
310  * filldir_reg_func - Report a directory entry to the caller of gfs2_dir_read()
311  * @opaque: opaque data used by the function
312  * @name: the name of the directory entry
313  * @length: the length of the name
314  * @offset: the entry's offset in the directory
315  * @inum: the inode number the entry points to
316  * @type: the type of inode the entry points to
317  *
318  * Returns: 0 on success, 1 if buffer full
319  */
320
321 static int filldir_reg_func(void *opaque, const char *name, unsigned int length,
322                             uint64_t offset, struct gfs2_inum *inum,
323                             unsigned int type)
324 {
325         struct filldir_reg *fdr = (struct filldir_reg *)opaque;
326         struct gfs2_sbd *sdp = fdr->fdr_sbd;
327         int error;
328
329         error = fdr->fdr_filldir(fdr->fdr_opaque, name, length, offset,
330                                  inum->no_formal_ino, type);
331         if (error)
332                 return 1;
333
334         if (fdr->fdr_prefetch && !(length == 1 && *name == '.')) {
335                 gfs2_glock_prefetch_num(sdp,
336                                        inum->no_addr, &gfs2_inode_glops,
337                                        LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
338                 gfs2_glock_prefetch_num(sdp,
339                                        inum->no_addr, &gfs2_iopen_glops,
340                                        LM_ST_SHARED, LM_FLAG_TRY);
341         }
342
343         return 0;
344 }
345
346 /**
347  * readdir_reg - Read directory entries from a directory
348  * @file: The directory to read from
349  * @dirent: Buffer for dirents
350  * @filldir: Function used to do the copying
351  *
352  * Returns: errno
353  */
354
355 static int readdir_reg(struct file *file, void *dirent, filldir_t filldir)
356 {
357         struct gfs2_inode *dip = get_v2ip(file->f_mapping->host);
358         struct filldir_reg fdr;
359         struct gfs2_holder d_gh;
360         uint64_t offset = file->f_pos;
361         int error;
362
363         fdr.fdr_sbd = dip->i_sbd;
364         fdr.fdr_prefetch = 1;
365         fdr.fdr_filldir = filldir;
366         fdr.fdr_opaque = dirent;
367
368         gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
369         error = gfs2_glock_nq_atime(&d_gh);
370         if (error) {
371                 gfs2_holder_uninit(&d_gh);
372                 return error;
373         }
374
375         error = gfs2_dir_read(dip, &offset, &fdr, filldir_reg_func);
376
377         gfs2_glock_dq_uninit(&d_gh);
378
379         file->f_pos = offset;
380
381         return error;
382 }
383
384 /**
385  * filldir_bad_func - Report a directory entry to the caller of gfs2_dir_read()
386  * @opaque: opaque data used by the function
387  * @name: the name of the directory entry
388  * @length: the length of the name
389  * @offset: the entry's offset in the directory
390  * @inum: the inode number the entry points to
391  * @type: the type of inode the entry points to
392  *
393  * For supporting NFS.
394  *
395  * Returns: 0 on success, 1 if buffer full
396  */
397
398 static int filldir_bad_func(void *opaque, const char *name, unsigned int length,
399                             uint64_t offset, struct gfs2_inum *inum,
400                             unsigned int type)
401 {
402         struct filldir_bad *fdb = (struct filldir_bad *)opaque;
403         struct gfs2_sbd *sdp = fdb->fdb_sbd;
404         struct filldir_bad_entry *fbe;
405
406         if (fdb->fdb_entry_off == fdb->fdb_entry_num ||
407             fdb->fdb_name_off + length > fdb->fdb_name_size)
408                 return 1;
409
410         fbe = &fdb->fdb_entry[fdb->fdb_entry_off];
411         fbe->fbe_name = fdb->fdb_name + fdb->fdb_name_off;
412         memcpy(fbe->fbe_name, name, length);
413         fbe->fbe_length = length;
414         fbe->fbe_offset = offset;
415         fbe->fbe_inum = *inum;
416         fbe->fbe_type = type;
417
418         fdb->fdb_entry_off++;
419         fdb->fdb_name_off += length;
420
421         if (!(length == 1 && *name == '.')) {
422                 gfs2_glock_prefetch_num(sdp,
423                                        inum->no_addr, &gfs2_inode_glops,
424                                        LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
425                 gfs2_glock_prefetch_num(sdp,
426                                        inum->no_addr, &gfs2_iopen_glops,
427                                        LM_ST_SHARED, LM_FLAG_TRY);
428         }
429
430         return 0;
431 }
432
433 /**
434  * readdir_bad - Read directory entries from a directory
435  * @file: The directory to read from
436  * @dirent: Buffer for dirents
437  * @filldir: Function used to do the copying
438  *
439  * For supporting NFS.
440  *
441  * Returns: errno
442  */
443
444 static int readdir_bad(struct file *file, void *dirent, filldir_t filldir)
445 {
446         struct gfs2_inode *dip = get_v2ip(file->f_mapping->host);
447         struct gfs2_sbd *sdp = dip->i_sbd;
448         struct filldir_reg fdr;
449         unsigned int entries, size;
450         struct filldir_bad *fdb;
451         struct gfs2_holder d_gh;
452         uint64_t offset = file->f_pos;
453         unsigned int x;
454         struct filldir_bad_entry *fbe;
455         int error;
456
457         entries = gfs2_tune_get(sdp, gt_entries_per_readdir);
458         size = sizeof(struct filldir_bad) +
459             entries * (sizeof(struct filldir_bad_entry) + GFS2_FAST_NAME_SIZE);
460
461         fdb = kzalloc(size, GFP_KERNEL);
462         if (!fdb)
463                 return -ENOMEM;
464
465         fdb->fdb_sbd = sdp;
466         fdb->fdb_entry = (struct filldir_bad_entry *)(fdb + 1);
467         fdb->fdb_entry_num = entries;
468         fdb->fdb_name = ((char *)fdb) + sizeof(struct filldir_bad) +
469                 entries * sizeof(struct filldir_bad_entry);
470         fdb->fdb_name_size = entries * GFS2_FAST_NAME_SIZE;
471
472         gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
473         error = gfs2_glock_nq_atime(&d_gh);
474         if (error) {
475                 gfs2_holder_uninit(&d_gh);
476                 goto out;
477         }
478
479         error = gfs2_dir_read(dip, &offset, fdb, filldir_bad_func);
480
481         gfs2_glock_dq_uninit(&d_gh);
482
483         fdr.fdr_sbd = sdp;
484         fdr.fdr_prefetch = 0;
485         fdr.fdr_filldir = filldir;
486         fdr.fdr_opaque = dirent;
487
488         for (x = 0; x < fdb->fdb_entry_off; x++) {
489                 fbe = &fdb->fdb_entry[x];
490
491                 error = filldir_reg_func(&fdr,
492                                          fbe->fbe_name, fbe->fbe_length,
493                                          fbe->fbe_offset,
494                                          &fbe->fbe_inum, fbe->fbe_type);
495                 if (error) {
496                         file->f_pos = fbe->fbe_offset;
497                         error = 0;
498                         goto out;
499                 }
500         }
501
502         file->f_pos = offset;
503
504  out:
505         kfree(fdb);
506
507         return error;
508 }
509
510 /**
511  * gfs2_readdir - Read directory entries from a directory
512  * @file: The directory to read from
513  * @dirent: Buffer for dirents
514  * @filldir: Function used to do the copying
515  *
516  * Returns: errno
517  */
518
519 static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
520 {
521         int error;
522
523         if (strcmp(current->comm, "nfsd") != 0)
524                 error = readdir_reg(file, dirent, filldir);
525         else
526                 error = readdir_bad(file, dirent, filldir);
527
528         return error;
529 }
530
531 static int gfs2_ioctl_flags(struct gfs2_inode *ip, unsigned int cmd,
532                             unsigned long arg)
533 {
534         unsigned int lmode = (cmd == GFS2_IOCTL_SETFLAGS) ?
535                              LM_ST_EXCLUSIVE : LM_ST_SHARED;
536         struct buffer_head *dibh;
537         struct gfs2_holder i_gh;
538         int error;
539         __u32 flags = 0, change;
540
541         if (cmd == GFS2_IOCTL_SETFLAGS) {
542                 error = get_user(flags, (__u32 __user *)arg);
543                 if (error)
544                         return -EFAULT;
545         }
546
547         error = gfs2_glock_nq_init(ip->i_gl, lmode, 0, &i_gh);
548         if (error)
549                 return error;
550
551         if (cmd == GFS2_IOCTL_SETFLAGS) {
552                 change = flags ^ ip->i_di.di_flags;
553                 error = -EPERM;
554                 if (change & (GFS2_DIF_IMMUTABLE|GFS2_DIF_APPENDONLY)) {
555                         if (!capable(CAP_LINUX_IMMUTABLE))
556                                 goto out;
557                 }
558                 error = -EINVAL;
559                 if (flags & (GFS2_DIF_JDATA|GFS2_DIF_DIRECTIO)) {
560                         if (!S_ISREG(ip->i_di.di_mode))
561                                 goto out;
562                 }
563                 if (flags &
564                     (GFS2_DIF_INHERIT_JDATA|GFS2_DIF_INHERIT_DIRECTIO)) {
565                         if (!S_ISDIR(ip->i_di.di_mode))
566                                 goto out;
567                 }
568
569                 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE, 0);
570                 if (error)
571                         goto out;
572
573                 error = gfs2_meta_inode_buffer(ip, &dibh);
574                 if (error)
575                         goto out_trans_end;
576
577                 ip->i_di.di_flags = flags;
578
579                 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
580                 gfs2_dinode_out(&ip->i_di, dibh->b_data);
581
582                 brelse(dibh);
583
584 out_trans_end:
585                 gfs2_trans_end(ip->i_sbd);
586         } else {
587                 flags = ip->i_di.di_flags;
588         }
589 out:
590         gfs2_glock_dq_uninit(&i_gh);
591         if (cmd == GFS2_IOCTL_GETFLAGS) {
592                 if (put_user(flags, (__u32 __user *)arg))
593                         return -EFAULT;
594         }
595         return error;
596 }
597
598 /**
599  * gfs2_ioctl - do an ioctl on a file
600  * @inode: the inode
601  * @file: the file pointer
602  * @cmd: the ioctl command
603  * @arg: the argument
604  *
605  * Returns: errno
606  */
607
608 static int gfs2_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
609                       unsigned long arg)
610 {
611         struct gfs2_inode *ip = get_v2ip(inode);
612
613         switch (cmd) {
614         case GFS2_IOCTL_SETFLAGS:
615         case GFS2_IOCTL_GETFLAGS:
616                 return gfs2_ioctl_flags(ip, cmd, arg);
617
618         default:
619                 return -ENOTTY;
620         }
621 }
622
623 /**
624  * gfs2_mmap -
625  * @file: The file to map
626  * @vma: The VMA which described the mapping
627  *
628  * Returns: 0 or error code
629  */
630
631 static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
632 {
633         struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
634         struct gfs2_holder i_gh;
635         int error;
636
637         gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
638         error = gfs2_glock_nq_atime(&i_gh);
639         if (error) {
640                 gfs2_holder_uninit(&i_gh);
641                 return error;
642         }
643
644         /* This is VM_MAYWRITE instead of VM_WRITE because a call
645            to mprotect() can turn on VM_WRITE later. */
646
647         if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
648             (VM_MAYSHARE | VM_MAYWRITE))
649                 vma->vm_ops = &gfs2_vm_ops_sharewrite;
650         else
651                 vma->vm_ops = &gfs2_vm_ops_private;
652
653         gfs2_glock_dq_uninit(&i_gh);
654
655         return error;
656 }
657
658 /**
659  * gfs2_open - open a file
660  * @inode: the inode to open
661  * @file: the struct file for this opening
662  *
663  * Returns: errno
664  */
665
666 static int gfs2_open(struct inode *inode, struct file *file)
667 {
668         struct gfs2_inode *ip = get_v2ip(inode);
669         struct gfs2_holder i_gh;
670         struct gfs2_file *fp;
671         int error;
672
673         fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
674         if (!fp)
675                 return -ENOMEM;
676
677         mutex_init(&fp->f_fl_mutex);
678
679         fp->f_inode = ip;
680         fp->f_vfile = file;
681
682         gfs2_assert_warn(ip->i_sbd, !get_v2fp(file));
683         set_v2fp(file, fp);
684
685         if (S_ISREG(ip->i_di.di_mode)) {
686                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
687                                            &i_gh);
688                 if (error)
689                         goto fail;
690
691                 if (!(file->f_flags & O_LARGEFILE) &&
692                     ip->i_di.di_size > MAX_NON_LFS) {
693                         error = -EFBIG;
694                         goto fail_gunlock;
695                 }
696
697                 /* Listen to the Direct I/O flag */
698
699                 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
700                         file->f_flags |= O_DIRECT;
701
702                 gfs2_glock_dq_uninit(&i_gh);
703         }
704
705         return 0;
706
707  fail_gunlock:
708         gfs2_glock_dq_uninit(&i_gh);
709
710  fail:
711         set_v2fp(file, NULL);
712         kfree(fp);
713
714         return error;
715 }
716
717 /**
718  * gfs2_close - called to close a struct file
719  * @inode: the inode the struct file belongs to
720  * @file: the struct file being closed
721  *
722  * Returns: errno
723  */
724
725 static int gfs2_close(struct inode *inode, struct file *file)
726 {
727         struct gfs2_sbd *sdp = get_v2sdp(inode->i_sb);
728         struct gfs2_file *fp;
729
730         fp = get_v2fp(file);
731         set_v2fp(file, NULL);
732
733         if (gfs2_assert_warn(sdp, fp))
734                 return -EIO;
735
736         kfree(fp);
737
738         return 0;
739 }
740
741 /**
742  * gfs2_fsync - sync the dirty data for a file (across the cluster)
743  * @file: the file that points to the dentry (we ignore this)
744  * @dentry: the dentry that points to the inode to sync
745  *
746  * Returns: errno
747  */
748
749 static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
750 {
751         struct gfs2_inode *ip = get_v2ip(dentry->d_inode);
752
753         gfs2_log_flush_glock(ip->i_gl);
754
755         return 0;
756 }
757
758 /**
759  * gfs2_lock - acquire/release a posix lock on a file
760  * @file: the file pointer
761  * @cmd: either modify or retrieve lock state, possibly wait
762  * @fl: type and range of lock
763  *
764  * Returns: errno
765  */
766
767 static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
768 {
769         struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
770         struct gfs2_sbd *sdp = ip->i_sbd;
771         struct lm_lockname name =
772                 { .ln_number = ip->i_num.no_addr,
773                   .ln_type = LM_TYPE_PLOCK };
774
775         if (!(fl->fl_flags & FL_POSIX))
776                 return -ENOLCK;
777         if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
778                 return -ENOLCK;
779
780         if (sdp->sd_args.ar_localflocks) {
781                 if (IS_GETLK(cmd)) {
782                         struct file_lock *tmp;
783                         lock_kernel();
784                         tmp = posix_test_lock(file, fl);
785                         fl->fl_type = F_UNLCK;
786                         if (tmp)
787                                 memcpy(fl, tmp, sizeof(struct file_lock));
788                         unlock_kernel();
789                         return 0;
790                 } else {
791                         int error;
792                         lock_kernel();
793                         error = posix_lock_file_wait(file, fl);
794                         unlock_kernel();
795                         return error;
796                 }
797         }
798
799         if (IS_GETLK(cmd))
800                 return gfs2_lm_plock_get(sdp, &name, file, fl);
801         else if (fl->fl_type == F_UNLCK)
802                 return gfs2_lm_punlock(sdp, &name, file, fl);
803         else
804                 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
805 }
806
807 /**
808  * gfs2_sendfile - Send bytes to a file or socket
809  * @in_file: The file to read from
810  * @out_file: The file to write to
811  * @count: The amount of data
812  * @offset: The beginning file offset
813  *
814  * Outputs: offset - updated according to number of bytes read
815  *
816  * Returns: The number of bytes sent, errno on failure
817  */
818
819 static ssize_t gfs2_sendfile(struct file *in_file, loff_t *offset, size_t count,
820                              read_actor_t actor, void *target)
821 {
822         return generic_file_sendfile(in_file, offset, count, actor, target);
823 }
824
825 static int do_flock(struct file *file, int cmd, struct file_lock *fl)
826 {
827         struct gfs2_file *fp = get_v2fp(file);
828         struct gfs2_holder *fl_gh = &fp->f_fl_gh;
829         struct gfs2_inode *ip = fp->f_inode;
830         struct gfs2_glock *gl;
831         unsigned int state;
832         int flags;
833         int error = 0;
834
835         state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
836         flags = ((IS_SETLKW(cmd)) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
837
838         mutex_lock(&fp->f_fl_mutex);
839
840         gl = fl_gh->gh_gl;
841         if (gl) {
842                 if (fl_gh->gh_state == state)
843                         goto out;
844                 gfs2_glock_hold(gl);
845                 flock_lock_file_wait(file,
846                                      &(struct file_lock){.fl_type = F_UNLCK});          
847                 gfs2_glock_dq_uninit(fl_gh);
848         } else {
849                 error = gfs2_glock_get(ip->i_sbd,
850                                       ip->i_num.no_addr, &gfs2_flock_glops,
851                                       CREATE, &gl);
852                 if (error)
853                         goto out;
854         }
855
856         gfs2_holder_init(gl, state, flags, fl_gh);
857         gfs2_glock_put(gl);
858
859         error = gfs2_glock_nq(fl_gh);
860         if (error) {
861                 gfs2_holder_uninit(fl_gh);
862                 if (error == GLR_TRYFAILED)
863                         error = -EAGAIN;
864         } else {
865                 error = flock_lock_file_wait(file, fl);
866                 gfs2_assert_warn(ip->i_sbd, !error);
867         }
868
869  out:
870         mutex_unlock(&fp->f_fl_mutex);
871
872         return error;
873 }
874
875 static void do_unflock(struct file *file, struct file_lock *fl)
876 {
877         struct gfs2_file *fp = get_v2fp(file);
878         struct gfs2_holder *fl_gh = &fp->f_fl_gh;
879
880         mutex_lock(&fp->f_fl_mutex);
881         flock_lock_file_wait(file, fl);
882         if (fl_gh->gh_gl)
883                 gfs2_glock_dq_uninit(fl_gh);
884         mutex_unlock(&fp->f_fl_mutex);
885 }
886
887 /**
888  * gfs2_flock - acquire/release a flock lock on a file
889  * @file: the file pointer
890  * @cmd: either modify or retrieve lock state, possibly wait
891  * @fl: type and range of lock
892  *
893  * Returns: errno
894  */
895
896 static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
897 {
898         struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
899         struct gfs2_sbd *sdp = ip->i_sbd;
900
901         if (!(fl->fl_flags & FL_FLOCK))
902                 return -ENOLCK;
903         if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
904                 return -ENOLCK;
905
906         if (sdp->sd_args.ar_localflocks)
907                 return flock_lock_file_wait(file, fl);
908
909         if (fl->fl_type == F_UNLCK) {
910                 do_unflock(file, fl);
911                 return 0;
912         } else
913                 return do_flock(file, cmd, fl);
914 }
915
916 struct file_operations gfs2_file_fops = {
917         .llseek = gfs2_llseek,
918         .read = gfs2_read,
919         .readv = gfs2_file_readv,
920         .aio_read = gfs2_file_aio_read,
921         .write = generic_file_write,
922         .writev = generic_file_writev,
923         .aio_write = generic_file_aio_write,
924         .ioctl = gfs2_ioctl,
925         .mmap = gfs2_mmap,
926         .open = gfs2_open,
927         .release = gfs2_close,
928         .fsync = gfs2_fsync,
929         .lock = gfs2_lock,
930         .sendfile = gfs2_sendfile,
931         .flock = gfs2_flock,
932 };
933
934 struct file_operations gfs2_dir_fops = {
935         .readdir = gfs2_readdir,
936         .ioctl = gfs2_ioctl,
937         .open = gfs2_open,
938         .release = gfs2_close,
939         .fsync = gfs2_fsync,
940         .lock = gfs2_lock,
941         .flock = gfs2_flock,
942 };
943