]> Git Repo - linux.git/blob - fs/gfs2/file.c
ionic: Mark error paths in the data path as unlikely
[linux.git] / fs / gfs2 / file.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
4  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
5  */
6
7 #include <linux/slab.h>
8 #include <linux/spinlock.h>
9 #include <linux/compat.h>
10 #include <linux/completion.h>
11 #include <linux/buffer_head.h>
12 #include <linux/pagemap.h>
13 #include <linux/uio.h>
14 #include <linux/blkdev.h>
15 #include <linux/mm.h>
16 #include <linux/mount.h>
17 #include <linux/fs.h>
18 #include <linux/filelock.h>
19 #include <linux/gfs2_ondisk.h>
20 #include <linux/falloc.h>
21 #include <linux/swap.h>
22 #include <linux/crc32.h>
23 #include <linux/writeback.h>
24 #include <linux/uaccess.h>
25 #include <linux/dlm.h>
26 #include <linux/dlm_plock.h>
27 #include <linux/delay.h>
28 #include <linux/backing-dev.h>
29 #include <linux/fileattr.h>
30
31 #include "gfs2.h"
32 #include "incore.h"
33 #include "bmap.h"
34 #include "aops.h"
35 #include "dir.h"
36 #include "glock.h"
37 #include "glops.h"
38 #include "inode.h"
39 #include "log.h"
40 #include "meta_io.h"
41 #include "quota.h"
42 #include "rgrp.h"
43 #include "trans.h"
44 #include "util.h"
45
46 /**
47  * gfs2_llseek - seek to a location in a file
48  * @file: the file
49  * @offset: the offset
50  * @whence: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
51  *
52  * SEEK_END requires the glock for the file because it references the
53  * file's size.
54  *
55  * Returns: The new offset, or errno
56  */
57
58 static loff_t gfs2_llseek(struct file *file, loff_t offset, int whence)
59 {
60         struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
61         struct gfs2_holder i_gh;
62         loff_t error;
63
64         switch (whence) {
65         case SEEK_END:
66                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
67                                            &i_gh);
68                 if (!error) {
69                         error = generic_file_llseek(file, offset, whence);
70                         gfs2_glock_dq_uninit(&i_gh);
71                 }
72                 break;
73
74         case SEEK_DATA:
75                 error = gfs2_seek_data(file, offset);
76                 break;
77
78         case SEEK_HOLE:
79                 error = gfs2_seek_hole(file, offset);
80                 break;
81
82         case SEEK_CUR:
83         case SEEK_SET:
84                 /*
85                  * These don't reference inode->i_size and don't depend on the
86                  * block mapping, so we don't need the glock.
87                  */
88                 error = generic_file_llseek(file, offset, whence);
89                 break;
90         default:
91                 error = -EINVAL;
92         }
93
94         return error;
95 }
96
97 /**
98  * gfs2_readdir - Iterator for a directory
99  * @file: The directory to read from
100  * @ctx: What to feed directory entries to
101  *
102  * Returns: errno
103  */
104
105 static int gfs2_readdir(struct file *file, struct dir_context *ctx)
106 {
107         struct inode *dir = file->f_mapping->host;
108         struct gfs2_inode *dip = GFS2_I(dir);
109         struct gfs2_holder d_gh;
110         int error;
111
112         error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
113         if (error)
114                 return error;
115
116         error = gfs2_dir_read(dir, ctx, &file->f_ra);
117
118         gfs2_glock_dq_uninit(&d_gh);
119
120         return error;
121 }
122
123 /*
124  * struct fsflag_gfs2flag
125  *
126  * The FS_JOURNAL_DATA_FL flag maps to GFS2_DIF_INHERIT_JDATA for directories,
127  * and to GFS2_DIF_JDATA for non-directories.
128  */
129 static struct {
130         u32 fsflag;
131         u32 gfsflag;
132 } fsflag_gfs2flag[] = {
133         {FS_SYNC_FL, GFS2_DIF_SYNC},
134         {FS_IMMUTABLE_FL, GFS2_DIF_IMMUTABLE},
135         {FS_APPEND_FL, GFS2_DIF_APPENDONLY},
136         {FS_NOATIME_FL, GFS2_DIF_NOATIME},
137         {FS_INDEX_FL, GFS2_DIF_EXHASH},
138         {FS_TOPDIR_FL, GFS2_DIF_TOPDIR},
139         {FS_JOURNAL_DATA_FL, GFS2_DIF_JDATA | GFS2_DIF_INHERIT_JDATA},
140 };
141
142 static inline u32 gfs2_gfsflags_to_fsflags(struct inode *inode, u32 gfsflags)
143 {
144         int i;
145         u32 fsflags = 0;
146
147         if (S_ISDIR(inode->i_mode))
148                 gfsflags &= ~GFS2_DIF_JDATA;
149         else
150                 gfsflags &= ~GFS2_DIF_INHERIT_JDATA;
151
152         for (i = 0; i < ARRAY_SIZE(fsflag_gfs2flag); i++)
153                 if (gfsflags & fsflag_gfs2flag[i].gfsflag)
154                         fsflags |= fsflag_gfs2flag[i].fsflag;
155         return fsflags;
156 }
157
158 int gfs2_fileattr_get(struct dentry *dentry, struct fileattr *fa)
159 {
160         struct inode *inode = d_inode(dentry);
161         struct gfs2_inode *ip = GFS2_I(inode);
162         struct gfs2_holder gh;
163         int error;
164         u32 fsflags;
165
166         if (d_is_special(dentry))
167                 return -ENOTTY;
168
169         gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
170         error = gfs2_glock_nq(&gh);
171         if (error)
172                 goto out_uninit;
173
174         fsflags = gfs2_gfsflags_to_fsflags(inode, ip->i_diskflags);
175
176         fileattr_fill_flags(fa, fsflags);
177
178         gfs2_glock_dq(&gh);
179 out_uninit:
180         gfs2_holder_uninit(&gh);
181         return error;
182 }
183
184 void gfs2_set_inode_flags(struct inode *inode)
185 {
186         struct gfs2_inode *ip = GFS2_I(inode);
187         unsigned int flags = inode->i_flags;
188
189         flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_NOSEC);
190         if ((ip->i_eattr == 0) && !is_sxid(inode->i_mode))
191                 flags |= S_NOSEC;
192         if (ip->i_diskflags & GFS2_DIF_IMMUTABLE)
193                 flags |= S_IMMUTABLE;
194         if (ip->i_diskflags & GFS2_DIF_APPENDONLY)
195                 flags |= S_APPEND;
196         if (ip->i_diskflags & GFS2_DIF_NOATIME)
197                 flags |= S_NOATIME;
198         if (ip->i_diskflags & GFS2_DIF_SYNC)
199                 flags |= S_SYNC;
200         inode->i_flags = flags;
201 }
202
203 /* Flags that can be set by user space */
204 #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA|                    \
205                              GFS2_DIF_IMMUTABLE|                \
206                              GFS2_DIF_APPENDONLY|               \
207                              GFS2_DIF_NOATIME|                  \
208                              GFS2_DIF_SYNC|                     \
209                              GFS2_DIF_TOPDIR|                   \
210                              GFS2_DIF_INHERIT_JDATA)
211
212 /**
213  * do_gfs2_set_flags - set flags on an inode
214  * @inode: The inode
215  * @reqflags: The flags to set
216  * @mask: Indicates which flags are valid
217  *
218  */
219 static int do_gfs2_set_flags(struct inode *inode, u32 reqflags, u32 mask)
220 {
221         struct gfs2_inode *ip = GFS2_I(inode);
222         struct gfs2_sbd *sdp = GFS2_SB(inode);
223         struct buffer_head *bh;
224         struct gfs2_holder gh;
225         int error;
226         u32 new_flags, flags;
227
228         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
229         if (error)
230                 return error;
231
232         error = 0;
233         flags = ip->i_diskflags;
234         new_flags = (flags & ~mask) | (reqflags & mask);
235         if ((new_flags ^ flags) == 0)
236                 goto out;
237
238         if (!IS_IMMUTABLE(inode)) {
239                 error = gfs2_permission(&nop_mnt_idmap, inode, MAY_WRITE);
240                 if (error)
241                         goto out;
242         }
243         if ((flags ^ new_flags) & GFS2_DIF_JDATA) {
244                 if (new_flags & GFS2_DIF_JDATA)
245                         gfs2_log_flush(sdp, ip->i_gl,
246                                        GFS2_LOG_HEAD_FLUSH_NORMAL |
247                                        GFS2_LFC_SET_FLAGS);
248                 error = filemap_fdatawrite(inode->i_mapping);
249                 if (error)
250                         goto out;
251                 error = filemap_fdatawait(inode->i_mapping);
252                 if (error)
253                         goto out;
254                 if (new_flags & GFS2_DIF_JDATA)
255                         gfs2_ordered_del_inode(ip);
256         }
257         error = gfs2_trans_begin(sdp, RES_DINODE, 0);
258         if (error)
259                 goto out;
260         error = gfs2_meta_inode_buffer(ip, &bh);
261         if (error)
262                 goto out_trans_end;
263         inode_set_ctime_current(inode);
264         gfs2_trans_add_meta(ip->i_gl, bh);
265         ip->i_diskflags = new_flags;
266         gfs2_dinode_out(ip, bh->b_data);
267         brelse(bh);
268         gfs2_set_inode_flags(inode);
269         gfs2_set_aops(inode);
270 out_trans_end:
271         gfs2_trans_end(sdp);
272 out:
273         gfs2_glock_dq_uninit(&gh);
274         return error;
275 }
276
277 int gfs2_fileattr_set(struct mnt_idmap *idmap,
278                       struct dentry *dentry, struct fileattr *fa)
279 {
280         struct inode *inode = d_inode(dentry);
281         u32 fsflags = fa->flags, gfsflags = 0;
282         u32 mask;
283         int i;
284
285         if (d_is_special(dentry))
286                 return -ENOTTY;
287
288         if (fileattr_has_fsx(fa))
289                 return -EOPNOTSUPP;
290
291         for (i = 0; i < ARRAY_SIZE(fsflag_gfs2flag); i++) {
292                 if (fsflags & fsflag_gfs2flag[i].fsflag) {
293                         fsflags &= ~fsflag_gfs2flag[i].fsflag;
294                         gfsflags |= fsflag_gfs2flag[i].gfsflag;
295                 }
296         }
297         if (fsflags || gfsflags & ~GFS2_FLAGS_USER_SET)
298                 return -EINVAL;
299
300         mask = GFS2_FLAGS_USER_SET;
301         if (S_ISDIR(inode->i_mode)) {
302                 mask &= ~GFS2_DIF_JDATA;
303         } else {
304                 /* The GFS2_DIF_TOPDIR flag is only valid for directories. */
305                 if (gfsflags & GFS2_DIF_TOPDIR)
306                         return -EINVAL;
307                 mask &= ~(GFS2_DIF_TOPDIR | GFS2_DIF_INHERIT_JDATA);
308         }
309
310         return do_gfs2_set_flags(inode, gfsflags, mask);
311 }
312
313 static int gfs2_getlabel(struct file *filp, char __user *label)
314 {
315         struct inode *inode = file_inode(filp);
316         struct gfs2_sbd *sdp = GFS2_SB(inode);
317
318         if (copy_to_user(label, sdp->sd_sb.sb_locktable, GFS2_LOCKNAME_LEN))
319                 return -EFAULT;
320
321         return 0;
322 }
323
324 static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
325 {
326         switch(cmd) {
327         case FITRIM:
328                 return gfs2_fitrim(filp, (void __user *)arg);
329         case FS_IOC_GETFSLABEL:
330                 return gfs2_getlabel(filp, (char __user *)arg);
331         }
332
333         return -ENOTTY;
334 }
335
336 #ifdef CONFIG_COMPAT
337 static long gfs2_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
338 {
339         switch(cmd) {
340         /* Keep this list in sync with gfs2_ioctl */
341         case FITRIM:
342         case FS_IOC_GETFSLABEL:
343                 break;
344         default:
345                 return -ENOIOCTLCMD;
346         }
347
348         return gfs2_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
349 }
350 #else
351 #define gfs2_compat_ioctl NULL
352 #endif
353
354 /**
355  * gfs2_size_hint - Give a hint to the size of a write request
356  * @filep: The struct file
357  * @offset: The file offset of the write
358  * @size: The length of the write
359  *
360  * When we are about to do a write, this function records the total
361  * write size in order to provide a suitable hint to the lower layers
362  * about how many blocks will be required.
363  *
364  */
365
366 static void gfs2_size_hint(struct file *filep, loff_t offset, size_t size)
367 {
368         struct inode *inode = file_inode(filep);
369         struct gfs2_sbd *sdp = GFS2_SB(inode);
370         struct gfs2_inode *ip = GFS2_I(inode);
371         size_t blks = (size + sdp->sd_sb.sb_bsize - 1) >> sdp->sd_sb.sb_bsize_shift;
372         int hint = min_t(size_t, INT_MAX, blks);
373
374         if (hint > atomic_read(&ip->i_sizehint))
375                 atomic_set(&ip->i_sizehint, hint);
376 }
377
378 /**
379  * gfs2_allocate_folio_backing - Allocate blocks for a write fault
380  * @folio: The (locked) folio to allocate backing for
381  * @length: Size of the allocation
382  *
383  * We try to allocate all the blocks required for the folio in one go.  This
384  * might fail for various reasons, so we keep trying until all the blocks to
385  * back this folio are allocated.  If some of the blocks are already allocated,
386  * that is ok too.
387  */
388 static int gfs2_allocate_folio_backing(struct folio *folio, size_t length)
389 {
390         u64 pos = folio_pos(folio);
391
392         do {
393                 struct iomap iomap = { };
394
395                 if (gfs2_iomap_alloc(folio->mapping->host, pos, length, &iomap))
396                         return -EIO;
397
398                 if (length < iomap.length)
399                         iomap.length = length;
400                 length -= iomap.length;
401                 pos += iomap.length;
402         } while (length > 0);
403
404         return 0;
405 }
406
407 /**
408  * gfs2_page_mkwrite - Make a shared, mmap()ed, page writable
409  * @vmf: The virtual memory fault containing the page to become writable
410  *
411  * When the page becomes writable, we need to ensure that we have
412  * blocks allocated on disk to back that page.
413  */
414
415 static vm_fault_t gfs2_page_mkwrite(struct vm_fault *vmf)
416 {
417         struct folio *folio = page_folio(vmf->page);
418         struct inode *inode = file_inode(vmf->vma->vm_file);
419         struct gfs2_inode *ip = GFS2_I(inode);
420         struct gfs2_sbd *sdp = GFS2_SB(inode);
421         struct gfs2_alloc_parms ap = {};
422         u64 pos = folio_pos(folio);
423         unsigned int data_blocks, ind_blocks, rblocks;
424         vm_fault_t ret = VM_FAULT_LOCKED;
425         struct gfs2_holder gh;
426         size_t length;
427         loff_t size;
428         int err;
429
430         sb_start_pagefault(inode->i_sb);
431
432         gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
433         err = gfs2_glock_nq(&gh);
434         if (err) {
435                 ret = vmf_fs_error(err);
436                 goto out_uninit;
437         }
438
439         /* Check folio index against inode size */
440         size = i_size_read(inode);
441         if (pos >= size) {
442                 ret = VM_FAULT_SIGBUS;
443                 goto out_unlock;
444         }
445
446         /* Update file times before taking folio lock */
447         file_update_time(vmf->vma->vm_file);
448
449         /* folio is wholly or partially inside EOF */
450         if (size - pos < folio_size(folio))
451                 length = size - pos;
452         else
453                 length = folio_size(folio);
454
455         gfs2_size_hint(vmf->vma->vm_file, pos, length);
456
457         set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
458         set_bit(GIF_SW_PAGED, &ip->i_flags);
459
460         /*
461          * iomap_writepage / iomap_writepages currently don't support inline
462          * files, so always unstuff here.
463          */
464
465         if (!gfs2_is_stuffed(ip) &&
466             !gfs2_write_alloc_required(ip, pos, length)) {
467                 folio_lock(folio);
468                 if (!folio_test_uptodate(folio) ||
469                     folio->mapping != inode->i_mapping) {
470                         ret = VM_FAULT_NOPAGE;
471                         folio_unlock(folio);
472                 }
473                 goto out_unlock;
474         }
475
476         err = gfs2_rindex_update(sdp);
477         if (err) {
478                 ret = vmf_fs_error(err);
479                 goto out_unlock;
480         }
481
482         gfs2_write_calc_reserv(ip, length, &data_blocks, &ind_blocks);
483         ap.target = data_blocks + ind_blocks;
484         err = gfs2_quota_lock_check(ip, &ap);
485         if (err) {
486                 ret = vmf_fs_error(err);
487                 goto out_unlock;
488         }
489         err = gfs2_inplace_reserve(ip, &ap);
490         if (err) {
491                 ret = vmf_fs_error(err);
492                 goto out_quota_unlock;
493         }
494
495         rblocks = RES_DINODE + ind_blocks;
496         if (gfs2_is_jdata(ip))
497                 rblocks += data_blocks ? data_blocks : 1;
498         if (ind_blocks || data_blocks) {
499                 rblocks += RES_STATFS + RES_QUOTA;
500                 rblocks += gfs2_rg_blocks(ip, data_blocks + ind_blocks);
501         }
502         err = gfs2_trans_begin(sdp, rblocks, 0);
503         if (err) {
504                 ret = vmf_fs_error(err);
505                 goto out_trans_fail;
506         }
507
508         /* Unstuff, if required, and allocate backing blocks for folio */
509         if (gfs2_is_stuffed(ip)) {
510                 err = gfs2_unstuff_dinode(ip);
511                 if (err) {
512                         ret = vmf_fs_error(err);
513                         goto out_trans_end;
514                 }
515         }
516
517         folio_lock(folio);
518         /* If truncated, we must retry the operation, we may have raced
519          * with the glock demotion code.
520          */
521         if (!folio_test_uptodate(folio) || folio->mapping != inode->i_mapping) {
522                 ret = VM_FAULT_NOPAGE;
523                 goto out_page_locked;
524         }
525
526         err = gfs2_allocate_folio_backing(folio, length);
527         if (err)
528                 ret = vmf_fs_error(err);
529
530 out_page_locked:
531         if (ret != VM_FAULT_LOCKED)
532                 folio_unlock(folio);
533 out_trans_end:
534         gfs2_trans_end(sdp);
535 out_trans_fail:
536         gfs2_inplace_release(ip);
537 out_quota_unlock:
538         gfs2_quota_unlock(ip);
539 out_unlock:
540         gfs2_glock_dq(&gh);
541 out_uninit:
542         gfs2_holder_uninit(&gh);
543         if (ret == VM_FAULT_LOCKED) {
544                 folio_mark_dirty(folio);
545                 folio_wait_stable(folio);
546         }
547         sb_end_pagefault(inode->i_sb);
548         return ret;
549 }
550
551 static vm_fault_t gfs2_fault(struct vm_fault *vmf)
552 {
553         struct inode *inode = file_inode(vmf->vma->vm_file);
554         struct gfs2_inode *ip = GFS2_I(inode);
555         struct gfs2_holder gh;
556         vm_fault_t ret;
557         int err;
558
559         gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
560         err = gfs2_glock_nq(&gh);
561         if (err) {
562                 ret = vmf_fs_error(err);
563                 goto out_uninit;
564         }
565         ret = filemap_fault(vmf);
566         gfs2_glock_dq(&gh);
567 out_uninit:
568         gfs2_holder_uninit(&gh);
569         return ret;
570 }
571
572 static const struct vm_operations_struct gfs2_vm_ops = {
573         .fault = gfs2_fault,
574         .map_pages = filemap_map_pages,
575         .page_mkwrite = gfs2_page_mkwrite,
576 };
577
578 /**
579  * gfs2_mmap
580  * @file: The file to map
581  * @vma: The VMA which described the mapping
582  *
583  * There is no need to get a lock here unless we should be updating
584  * atime. We ignore any locking errors since the only consequence is
585  * a missed atime update (which will just be deferred until later).
586  *
587  * Returns: 0
588  */
589
590 static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
591 {
592         struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
593
594         if (!(file->f_flags & O_NOATIME) &&
595             !IS_NOATIME(&ip->i_inode)) {
596                 struct gfs2_holder i_gh;
597                 int error;
598
599                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
600                                            &i_gh);
601                 if (error)
602                         return error;
603                 /* grab lock to update inode */
604                 gfs2_glock_dq_uninit(&i_gh);
605                 file_accessed(file);
606         }
607         vma->vm_ops = &gfs2_vm_ops;
608
609         return 0;
610 }
611
612 /**
613  * gfs2_open_common - This is common to open and atomic_open
614  * @inode: The inode being opened
615  * @file: The file being opened
616  *
617  * This maybe called under a glock or not depending upon how it has
618  * been called. We must always be called under a glock for regular
619  * files, however. For other file types, it does not matter whether
620  * we hold the glock or not.
621  *
622  * Returns: Error code or 0 for success
623  */
624
625 int gfs2_open_common(struct inode *inode, struct file *file)
626 {
627         struct gfs2_file *fp;
628         int ret;
629
630         if (S_ISREG(inode->i_mode)) {
631                 ret = generic_file_open(inode, file);
632                 if (ret)
633                         return ret;
634
635                 if (!gfs2_is_jdata(GFS2_I(inode)))
636                         file->f_mode |= FMODE_CAN_ODIRECT;
637         }
638
639         fp = kzalloc(sizeof(struct gfs2_file), GFP_NOFS);
640         if (!fp)
641                 return -ENOMEM;
642
643         mutex_init(&fp->f_fl_mutex);
644
645         gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
646         file->private_data = fp;
647         if (file->f_mode & FMODE_WRITE) {
648                 ret = gfs2_qa_get(GFS2_I(inode));
649                 if (ret)
650                         goto fail;
651         }
652         return 0;
653
654 fail:
655         kfree(file->private_data);
656         file->private_data = NULL;
657         return ret;
658 }
659
660 /**
661  * gfs2_open - open a file
662  * @inode: the inode to open
663  * @file: the struct file for this opening
664  *
665  * After atomic_open, this function is only used for opening files
666  * which are already cached. We must still get the glock for regular
667  * files to ensure that we have the file size uptodate for the large
668  * file check which is in the common code. That is only an issue for
669  * regular files though.
670  *
671  * Returns: errno
672  */
673
674 static int gfs2_open(struct inode *inode, struct file *file)
675 {
676         struct gfs2_inode *ip = GFS2_I(inode);
677         struct gfs2_holder i_gh;
678         int error;
679         bool need_unlock = false;
680
681         if (S_ISREG(ip->i_inode.i_mode)) {
682                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
683                                            &i_gh);
684                 if (error)
685                         return error;
686                 need_unlock = true;
687         }
688
689         error = gfs2_open_common(inode, file);
690
691         if (need_unlock)
692                 gfs2_glock_dq_uninit(&i_gh);
693
694         return error;
695 }
696
697 /**
698  * gfs2_release - called to close a struct file
699  * @inode: the inode the struct file belongs to
700  * @file: the struct file being closed
701  *
702  * Returns: errno
703  */
704
705 static int gfs2_release(struct inode *inode, struct file *file)
706 {
707         struct gfs2_inode *ip = GFS2_I(inode);
708
709         kfree(file->private_data);
710         file->private_data = NULL;
711
712         if (file->f_mode & FMODE_WRITE) {
713                 if (gfs2_rs_active(&ip->i_res))
714                         gfs2_rs_delete(ip);
715                 gfs2_qa_put(ip);
716         }
717         return 0;
718 }
719
720 /**
721  * gfs2_fsync - sync the dirty data for a file (across the cluster)
722  * @file: the file that points to the dentry
723  * @start: the start position in the file to sync
724  * @end: the end position in the file to sync
725  * @datasync: set if we can ignore timestamp changes
726  *
727  * We split the data flushing here so that we don't wait for the data
728  * until after we've also sent the metadata to disk. Note that for
729  * data=ordered, we will write & wait for the data at the log flush
730  * stage anyway, so this is unlikely to make much of a difference
731  * except in the data=writeback case.
732  *
733  * If the fdatawrite fails due to any reason except -EIO, we will
734  * continue the remainder of the fsync, although we'll still report
735  * the error at the end. This is to match filemap_write_and_wait_range()
736  * behaviour.
737  *
738  * Returns: errno
739  */
740
741 static int gfs2_fsync(struct file *file, loff_t start, loff_t end,
742                       int datasync)
743 {
744         struct address_space *mapping = file->f_mapping;
745         struct inode *inode = mapping->host;
746         int sync_state = inode->i_state & I_DIRTY;
747         struct gfs2_inode *ip = GFS2_I(inode);
748         int ret = 0, ret1 = 0;
749
750         if (mapping->nrpages) {
751                 ret1 = filemap_fdatawrite_range(mapping, start, end);
752                 if (ret1 == -EIO)
753                         return ret1;
754         }
755
756         if (!gfs2_is_jdata(ip))
757                 sync_state &= ~I_DIRTY_PAGES;
758         if (datasync)
759                 sync_state &= ~I_DIRTY_SYNC;
760
761         if (sync_state) {
762                 ret = sync_inode_metadata(inode, 1);
763                 if (ret)
764                         return ret;
765                 if (gfs2_is_jdata(ip))
766                         ret = file_write_and_wait(file);
767                 if (ret)
768                         return ret;
769                 gfs2_ail_flush(ip->i_gl, 1);
770         }
771
772         if (mapping->nrpages)
773                 ret = file_fdatawait_range(file, start, end);
774
775         return ret ? ret : ret1;
776 }
777
778 static inline bool should_fault_in_pages(struct iov_iter *i,
779                                          struct kiocb *iocb,
780                                          size_t *prev_count,
781                                          size_t *window_size)
782 {
783         size_t count = iov_iter_count(i);
784         size_t size, offs;
785
786         if (!count)
787                 return false;
788         if (!user_backed_iter(i))
789                 return false;
790
791         /*
792          * Try to fault in multiple pages initially.  When that doesn't result
793          * in any progress, fall back to a single page.
794          */
795         size = PAGE_SIZE;
796         offs = offset_in_page(iocb->ki_pos);
797         if (*prev_count != count) {
798                 size_t nr_dirtied;
799
800                 nr_dirtied = max(current->nr_dirtied_pause -
801                                  current->nr_dirtied, 8);
802                 size = min_t(size_t, SZ_1M, nr_dirtied << PAGE_SHIFT);
803         }
804
805         *prev_count = count;
806         *window_size = size - offs;
807         return true;
808 }
809
810 static ssize_t gfs2_file_direct_read(struct kiocb *iocb, struct iov_iter *to,
811                                      struct gfs2_holder *gh)
812 {
813         struct file *file = iocb->ki_filp;
814         struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
815         size_t prev_count = 0, window_size = 0;
816         size_t read = 0;
817         ssize_t ret;
818
819         /*
820          * In this function, we disable page faults when we're holding the
821          * inode glock while doing I/O.  If a page fault occurs, we indicate
822          * that the inode glock may be dropped, fault in the pages manually,
823          * and retry.
824          *
825          * Unlike generic_file_read_iter, for reads, iomap_dio_rw can trigger
826          * physical as well as manual page faults, and we need to disable both
827          * kinds.
828          *
829          * For direct I/O, gfs2 takes the inode glock in deferred mode.  This
830          * locking mode is compatible with other deferred holders, so multiple
831          * processes and nodes can do direct I/O to a file at the same time.
832          * There's no guarantee that reads or writes will be atomic.  Any
833          * coordination among readers and writers needs to happen externally.
834          */
835
836         if (!iov_iter_count(to))
837                 return 0; /* skip atime */
838
839         gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, 0, gh);
840 retry:
841         ret = gfs2_glock_nq(gh);
842         if (ret)
843                 goto out_uninit;
844         pagefault_disable();
845         to->nofault = true;
846         ret = iomap_dio_rw(iocb, to, &gfs2_iomap_ops, NULL,
847                            IOMAP_DIO_PARTIAL, NULL, read);
848         to->nofault = false;
849         pagefault_enable();
850         if (ret <= 0 && ret != -EFAULT)
851                 goto out_unlock;
852         /* No increment (+=) because iomap_dio_rw returns a cumulative value. */
853         if (ret > 0)
854                 read = ret;
855
856         if (should_fault_in_pages(to, iocb, &prev_count, &window_size)) {
857                 gfs2_glock_dq(gh);
858                 window_size -= fault_in_iov_iter_writeable(to, window_size);
859                 if (window_size)
860                         goto retry;
861         }
862 out_unlock:
863         if (gfs2_holder_queued(gh))
864                 gfs2_glock_dq(gh);
865 out_uninit:
866         gfs2_holder_uninit(gh);
867         /* User space doesn't expect partial success. */
868         if (ret < 0)
869                 return ret;
870         return read;
871 }
872
873 static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from,
874                                       struct gfs2_holder *gh)
875 {
876         struct file *file = iocb->ki_filp;
877         struct inode *inode = file->f_mapping->host;
878         struct gfs2_inode *ip = GFS2_I(inode);
879         size_t prev_count = 0, window_size = 0;
880         size_t written = 0;
881         bool enough_retries;
882         ssize_t ret;
883
884         /*
885          * In this function, we disable page faults when we're holding the
886          * inode glock while doing I/O.  If a page fault occurs, we indicate
887          * that the inode glock may be dropped, fault in the pages manually,
888          * and retry.
889          *
890          * For writes, iomap_dio_rw only triggers manual page faults, so we
891          * don't need to disable physical ones.
892          */
893
894         /*
895          * Deferred lock, even if its a write, since we do no allocation on
896          * this path. All we need to change is the atime, and this lock mode
897          * ensures that other nodes have flushed their buffered read caches
898          * (i.e. their page cache entries for this inode). We do not,
899          * unfortunately, have the option of only flushing a range like the
900          * VFS does.
901          */
902         gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, 0, gh);
903 retry:
904         ret = gfs2_glock_nq(gh);
905         if (ret)
906                 goto out_uninit;
907         /* Silently fall back to buffered I/O when writing beyond EOF */
908         if (iocb->ki_pos + iov_iter_count(from) > i_size_read(&ip->i_inode))
909                 goto out_unlock;
910
911         from->nofault = true;
912         ret = iomap_dio_rw(iocb, from, &gfs2_iomap_ops, NULL,
913                            IOMAP_DIO_PARTIAL, NULL, written);
914         from->nofault = false;
915         if (ret <= 0) {
916                 if (ret == -ENOTBLK)
917                         ret = 0;
918                 if (ret != -EFAULT)
919                         goto out_unlock;
920         }
921         /* No increment (+=) because iomap_dio_rw returns a cumulative value. */
922         if (ret > 0)
923                 written = ret;
924
925         enough_retries = prev_count == iov_iter_count(from) &&
926                          window_size <= PAGE_SIZE;
927         if (should_fault_in_pages(from, iocb, &prev_count, &window_size)) {
928                 gfs2_glock_dq(gh);
929                 window_size -= fault_in_iov_iter_readable(from, window_size);
930                 if (window_size) {
931                         if (!enough_retries)
932                                 goto retry;
933                         /* fall back to buffered I/O */
934                         ret = 0;
935                 }
936         }
937 out_unlock:
938         if (gfs2_holder_queued(gh))
939                 gfs2_glock_dq(gh);
940 out_uninit:
941         gfs2_holder_uninit(gh);
942         /* User space doesn't expect partial success. */
943         if (ret < 0)
944                 return ret;
945         return written;
946 }
947
948 static ssize_t gfs2_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
949 {
950         struct gfs2_inode *ip;
951         struct gfs2_holder gh;
952         size_t prev_count = 0, window_size = 0;
953         size_t read = 0;
954         ssize_t ret;
955
956         /*
957          * In this function, we disable page faults when we're holding the
958          * inode glock while doing I/O.  If a page fault occurs, we indicate
959          * that the inode glock may be dropped, fault in the pages manually,
960          * and retry.
961          */
962
963         if (iocb->ki_flags & IOCB_DIRECT)
964                 return gfs2_file_direct_read(iocb, to, &gh);
965
966         pagefault_disable();
967         iocb->ki_flags |= IOCB_NOIO;
968         ret = generic_file_read_iter(iocb, to);
969         iocb->ki_flags &= ~IOCB_NOIO;
970         pagefault_enable();
971         if (ret >= 0) {
972                 if (!iov_iter_count(to))
973                         return ret;
974                 read = ret;
975         } else if (ret != -EFAULT) {
976                 if (ret != -EAGAIN)
977                         return ret;
978                 if (iocb->ki_flags & IOCB_NOWAIT)
979                         return ret;
980         }
981         ip = GFS2_I(iocb->ki_filp->f_mapping->host);
982         gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
983 retry:
984         ret = gfs2_glock_nq(&gh);
985         if (ret)
986                 goto out_uninit;
987         pagefault_disable();
988         ret = generic_file_read_iter(iocb, to);
989         pagefault_enable();
990         if (ret <= 0 && ret != -EFAULT)
991                 goto out_unlock;
992         if (ret > 0)
993                 read += ret;
994
995         if (should_fault_in_pages(to, iocb, &prev_count, &window_size)) {
996                 gfs2_glock_dq(&gh);
997                 window_size -= fault_in_iov_iter_writeable(to, window_size);
998                 if (window_size)
999                         goto retry;
1000         }
1001 out_unlock:
1002         if (gfs2_holder_queued(&gh))
1003                 gfs2_glock_dq(&gh);
1004 out_uninit:
1005         gfs2_holder_uninit(&gh);
1006         return read ? read : ret;
1007 }
1008
1009 static ssize_t gfs2_file_buffered_write(struct kiocb *iocb,
1010                                         struct iov_iter *from,
1011                                         struct gfs2_holder *gh)
1012 {
1013         struct file *file = iocb->ki_filp;
1014         struct inode *inode = file_inode(file);
1015         struct gfs2_inode *ip = GFS2_I(inode);
1016         struct gfs2_sbd *sdp = GFS2_SB(inode);
1017         struct gfs2_holder *statfs_gh = NULL;
1018         size_t prev_count = 0, window_size = 0;
1019         size_t orig_count = iov_iter_count(from);
1020         size_t written = 0;
1021         ssize_t ret;
1022
1023         /*
1024          * In this function, we disable page faults when we're holding the
1025          * inode glock while doing I/O.  If a page fault occurs, we indicate
1026          * that the inode glock may be dropped, fault in the pages manually,
1027          * and retry.
1028          */
1029
1030         if (inode == sdp->sd_rindex) {
1031                 statfs_gh = kmalloc(sizeof(*statfs_gh), GFP_NOFS);
1032                 if (!statfs_gh)
1033                         return -ENOMEM;
1034         }
1035
1036         gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, gh);
1037         if (should_fault_in_pages(from, iocb, &prev_count, &window_size)) {
1038 retry:
1039                 window_size -= fault_in_iov_iter_readable(from, window_size);
1040                 if (!window_size) {
1041                         ret = -EFAULT;
1042                         goto out_uninit;
1043                 }
1044                 from->count = min(from->count, window_size);
1045         }
1046         ret = gfs2_glock_nq(gh);
1047         if (ret)
1048                 goto out_uninit;
1049
1050         if (inode == sdp->sd_rindex) {
1051                 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
1052
1053                 ret = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE,
1054                                          GL_NOCACHE, statfs_gh);
1055                 if (ret)
1056                         goto out_unlock;
1057         }
1058
1059         pagefault_disable();
1060         ret = iomap_file_buffered_write(iocb, from, &gfs2_iomap_ops);
1061         pagefault_enable();
1062         if (ret > 0)
1063                 written += ret;
1064
1065         if (inode == sdp->sd_rindex)
1066                 gfs2_glock_dq_uninit(statfs_gh);
1067
1068         if (ret <= 0 && ret != -EFAULT)
1069                 goto out_unlock;
1070
1071         from->count = orig_count - written;
1072         if (should_fault_in_pages(from, iocb, &prev_count, &window_size)) {
1073                 gfs2_glock_dq(gh);
1074                 goto retry;
1075         }
1076 out_unlock:
1077         if (gfs2_holder_queued(gh))
1078                 gfs2_glock_dq(gh);
1079 out_uninit:
1080         gfs2_holder_uninit(gh);
1081         kfree(statfs_gh);
1082         from->count = orig_count - written;
1083         return written ? written : ret;
1084 }
1085
1086 /**
1087  * gfs2_file_write_iter - Perform a write to a file
1088  * @iocb: The io context
1089  * @from: The data to write
1090  *
1091  * We have to do a lock/unlock here to refresh the inode size for
1092  * O_APPEND writes, otherwise we can land up writing at the wrong
1093  * offset. There is still a race, but provided the app is using its
1094  * own file locking, this will make O_APPEND work as expected.
1095  *
1096  */
1097
1098 static ssize_t gfs2_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1099 {
1100         struct file *file = iocb->ki_filp;
1101         struct inode *inode = file_inode(file);
1102         struct gfs2_inode *ip = GFS2_I(inode);
1103         struct gfs2_holder gh;
1104         ssize_t ret;
1105
1106         gfs2_size_hint(file, iocb->ki_pos, iov_iter_count(from));
1107
1108         if (iocb->ki_flags & IOCB_APPEND) {
1109                 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1110                 if (ret)
1111                         return ret;
1112                 gfs2_glock_dq_uninit(&gh);
1113         }
1114
1115         inode_lock(inode);
1116         ret = generic_write_checks(iocb, from);
1117         if (ret <= 0)
1118                 goto out_unlock;
1119
1120         ret = file_remove_privs(file);
1121         if (ret)
1122                 goto out_unlock;
1123
1124         if (iocb->ki_flags & IOCB_DIRECT) {
1125                 struct address_space *mapping = file->f_mapping;
1126                 ssize_t buffered, ret2;
1127
1128                 /*
1129                  * Note that under direct I/O, we don't allow and inode
1130                  * timestamp updates, so we're not calling file_update_time()
1131                  * here.
1132                  */
1133
1134                 ret = gfs2_file_direct_write(iocb, from, &gh);
1135                 if (ret < 0 || !iov_iter_count(from))
1136                         goto out_unlock;
1137
1138                 iocb->ki_flags |= IOCB_DSYNC;
1139                 buffered = gfs2_file_buffered_write(iocb, from, &gh);
1140                 if (unlikely(buffered <= 0)) {
1141                         if (!ret)
1142                                 ret = buffered;
1143                         goto out_unlock;
1144                 }
1145
1146                 /*
1147                  * We need to ensure that the page cache pages are written to
1148                  * disk and invalidated to preserve the expected O_DIRECT
1149                  * semantics.  If the writeback or invalidate fails, only report
1150                  * the direct I/O range as we don't know if the buffered pages
1151                  * made it to disk.
1152                  */
1153                 ret2 = generic_write_sync(iocb, buffered);
1154                 invalidate_mapping_pages(mapping,
1155                                 (iocb->ki_pos - buffered) >> PAGE_SHIFT,
1156                                 (iocb->ki_pos - 1) >> PAGE_SHIFT);
1157                 if (!ret || ret2 > 0)
1158                         ret += ret2;
1159         } else {
1160                 ret = file_update_time(file);
1161                 if (ret)
1162                         goto out_unlock;
1163
1164                 ret = gfs2_file_buffered_write(iocb, from, &gh);
1165                 if (likely(ret > 0))
1166                         ret = generic_write_sync(iocb, ret);
1167         }
1168
1169 out_unlock:
1170         inode_unlock(inode);
1171         return ret;
1172 }
1173
1174 static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
1175                            int mode)
1176 {
1177         struct super_block *sb = inode->i_sb;
1178         struct gfs2_inode *ip = GFS2_I(inode);
1179         loff_t end = offset + len;
1180         struct buffer_head *dibh;
1181         int error;
1182
1183         error = gfs2_meta_inode_buffer(ip, &dibh);
1184         if (unlikely(error))
1185                 return error;
1186
1187         gfs2_trans_add_meta(ip->i_gl, dibh);
1188
1189         if (gfs2_is_stuffed(ip)) {
1190                 error = gfs2_unstuff_dinode(ip);
1191                 if (unlikely(error))
1192                         goto out;
1193         }
1194
1195         while (offset < end) {
1196                 struct iomap iomap = { };
1197
1198                 error = gfs2_iomap_alloc(inode, offset, end - offset, &iomap);
1199                 if (error)
1200                         goto out;
1201                 offset = iomap.offset + iomap.length;
1202                 if (!(iomap.flags & IOMAP_F_NEW))
1203                         continue;
1204                 error = sb_issue_zeroout(sb, iomap.addr >> inode->i_blkbits,
1205                                          iomap.length >> inode->i_blkbits,
1206                                          GFP_NOFS);
1207                 if (error) {
1208                         fs_err(GFS2_SB(inode), "Failed to zero data buffers\n");
1209                         goto out;
1210                 }
1211         }
1212 out:
1213         brelse(dibh);
1214         return error;
1215 }
1216
1217 /**
1218  * calc_max_reserv() - Reverse of write_calc_reserv. Given a number of
1219  *                     blocks, determine how many bytes can be written.
1220  * @ip:          The inode in question.
1221  * @len:         Max cap of bytes. What we return in *len must be <= this.
1222  * @data_blocks: Compute and return the number of data blocks needed
1223  * @ind_blocks:  Compute and return the number of indirect blocks needed
1224  * @max_blocks:  The total blocks available to work with.
1225  *
1226  * Returns: void, but @len, @data_blocks and @ind_blocks are filled in.
1227  */
1228 static void calc_max_reserv(struct gfs2_inode *ip, loff_t *len,
1229                             unsigned int *data_blocks, unsigned int *ind_blocks,
1230                             unsigned int max_blocks)
1231 {
1232         loff_t max = *len;
1233         const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1234         unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
1235
1236         for (tmp = max_data; tmp > sdp->sd_diptrs;) {
1237                 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
1238                 max_data -= tmp;
1239         }
1240
1241         *data_blocks = max_data;
1242         *ind_blocks = max_blocks - max_data;
1243         *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
1244         if (*len > max) {
1245                 *len = max;
1246                 gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
1247         }
1248 }
1249
1250 static long __gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
1251 {
1252         struct inode *inode = file_inode(file);
1253         struct gfs2_sbd *sdp = GFS2_SB(inode);
1254         struct gfs2_inode *ip = GFS2_I(inode);
1255         struct gfs2_alloc_parms ap = {};
1256         unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
1257         loff_t bytes, max_bytes, max_blks;
1258         int error;
1259         const loff_t pos = offset;
1260         const loff_t count = len;
1261         loff_t bsize_mask = ~((loff_t)sdp->sd_sb.sb_bsize - 1);
1262         loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
1263         loff_t max_chunk_size = UINT_MAX & bsize_mask;
1264
1265         next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
1266
1267         offset &= bsize_mask;
1268
1269         len = next - offset;
1270         bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
1271         if (!bytes)
1272                 bytes = UINT_MAX;
1273         bytes &= bsize_mask;
1274         if (bytes == 0)
1275                 bytes = sdp->sd_sb.sb_bsize;
1276
1277         gfs2_size_hint(file, offset, len);
1278
1279         gfs2_write_calc_reserv(ip, PAGE_SIZE, &data_blocks, &ind_blocks);
1280         ap.min_target = data_blocks + ind_blocks;
1281
1282         while (len > 0) {
1283                 if (len < bytes)
1284                         bytes = len;
1285                 if (!gfs2_write_alloc_required(ip, offset, bytes)) {
1286                         len -= bytes;
1287                         offset += bytes;
1288                         continue;
1289                 }
1290
1291                 /* We need to determine how many bytes we can actually
1292                  * fallocate without exceeding quota or going over the
1293                  * end of the fs. We start off optimistically by assuming
1294                  * we can write max_bytes */
1295                 max_bytes = (len > max_chunk_size) ? max_chunk_size : len;
1296
1297                 /* Since max_bytes is most likely a theoretical max, we
1298                  * calculate a more realistic 'bytes' to serve as a good
1299                  * starting point for the number of bytes we may be able
1300                  * to write */
1301                 gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
1302                 ap.target = data_blocks + ind_blocks;
1303
1304                 error = gfs2_quota_lock_check(ip, &ap);
1305                 if (error)
1306                         return error;
1307                 /* ap.allowed tells us how many blocks quota will allow
1308                  * us to write. Check if this reduces max_blks */
1309                 max_blks = UINT_MAX;
1310                 if (ap.allowed)
1311                         max_blks = ap.allowed;
1312
1313                 error = gfs2_inplace_reserve(ip, &ap);
1314                 if (error)
1315                         goto out_qunlock;
1316
1317                 /* check if the selected rgrp limits our max_blks further */
1318                 if (ip->i_res.rs_reserved < max_blks)
1319                         max_blks = ip->i_res.rs_reserved;
1320
1321                 /* Almost done. Calculate bytes that can be written using
1322                  * max_blks. We also recompute max_bytes, data_blocks and
1323                  * ind_blocks */
1324                 calc_max_reserv(ip, &max_bytes, &data_blocks,
1325                                 &ind_blocks, max_blks);
1326
1327                 rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
1328                           RES_RG_HDR + gfs2_rg_blocks(ip, data_blocks + ind_blocks);
1329                 if (gfs2_is_jdata(ip))
1330                         rblocks += data_blocks ? data_blocks : 1;
1331
1332                 error = gfs2_trans_begin(sdp, rblocks,
1333                                          PAGE_SIZE >> inode->i_blkbits);
1334                 if (error)
1335                         goto out_trans_fail;
1336
1337                 error = fallocate_chunk(inode, offset, max_bytes, mode);
1338                 gfs2_trans_end(sdp);
1339
1340                 if (error)
1341                         goto out_trans_fail;
1342
1343                 len -= max_bytes;
1344                 offset += max_bytes;
1345                 gfs2_inplace_release(ip);
1346                 gfs2_quota_unlock(ip);
1347         }
1348
1349         if (!(mode & FALLOC_FL_KEEP_SIZE) && (pos + count) > inode->i_size)
1350                 i_size_write(inode, pos + count);
1351         file_update_time(file);
1352         mark_inode_dirty(inode);
1353
1354         if ((file->f_flags & O_DSYNC) || IS_SYNC(file->f_mapping->host))
1355                 return vfs_fsync_range(file, pos, pos + count - 1,
1356                                (file->f_flags & __O_SYNC) ? 0 : 1);
1357         return 0;
1358
1359 out_trans_fail:
1360         gfs2_inplace_release(ip);
1361 out_qunlock:
1362         gfs2_quota_unlock(ip);
1363         return error;
1364 }
1365
1366 static long gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
1367 {
1368         struct inode *inode = file_inode(file);
1369         struct gfs2_sbd *sdp = GFS2_SB(inode);
1370         struct gfs2_inode *ip = GFS2_I(inode);
1371         struct gfs2_holder gh;
1372         int ret;
1373
1374         if (mode & ~(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE))
1375                 return -EOPNOTSUPP;
1376         /* fallocate is needed by gfs2_grow to reserve space in the rindex */
1377         if (gfs2_is_jdata(ip) && inode != sdp->sd_rindex)
1378                 return -EOPNOTSUPP;
1379
1380         inode_lock(inode);
1381
1382         gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1383         ret = gfs2_glock_nq(&gh);
1384         if (ret)
1385                 goto out_uninit;
1386
1387         if (!(mode & FALLOC_FL_KEEP_SIZE) &&
1388             (offset + len) > inode->i_size) {
1389                 ret = inode_newsize_ok(inode, offset + len);
1390                 if (ret)
1391                         goto out_unlock;
1392         }
1393
1394         ret = get_write_access(inode);
1395         if (ret)
1396                 goto out_unlock;
1397
1398         if (mode & FALLOC_FL_PUNCH_HOLE) {
1399                 ret = __gfs2_punch_hole(file, offset, len);
1400         } else {
1401                 ret = __gfs2_fallocate(file, mode, offset, len);
1402                 if (ret)
1403                         gfs2_rs_deltree(&ip->i_res);
1404         }
1405
1406         put_write_access(inode);
1407 out_unlock:
1408         gfs2_glock_dq(&gh);
1409 out_uninit:
1410         gfs2_holder_uninit(&gh);
1411         inode_unlock(inode);
1412         return ret;
1413 }
1414
1415 static ssize_t gfs2_file_splice_write(struct pipe_inode_info *pipe,
1416                                       struct file *out, loff_t *ppos,
1417                                       size_t len, unsigned int flags)
1418 {
1419         ssize_t ret;
1420
1421         gfs2_size_hint(out, *ppos, len);
1422
1423         ret = iter_file_splice_write(pipe, out, ppos, len, flags);
1424         return ret;
1425 }
1426
1427 #ifdef CONFIG_GFS2_FS_LOCKING_DLM
1428
1429 /**
1430  * gfs2_lock - acquire/release a posix lock on a file
1431  * @file: the file pointer
1432  * @cmd: either modify or retrieve lock state, possibly wait
1433  * @fl: type and range of lock
1434  *
1435  * Returns: errno
1436  */
1437
1438 static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
1439 {
1440         struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
1441         struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
1442         struct lm_lockstruct *ls = &sdp->sd_lockstruct;
1443
1444         if (!(fl->c.flc_flags & FL_POSIX))
1445                 return -ENOLCK;
1446         if (gfs2_withdrawing_or_withdrawn(sdp)) {
1447                 if (lock_is_unlock(fl))
1448                         locks_lock_file_wait(file, fl);
1449                 return -EIO;
1450         }
1451         if (cmd == F_CANCELLK)
1452                 return dlm_posix_cancel(ls->ls_dlm, ip->i_no_addr, file, fl);
1453         else if (IS_GETLK(cmd))
1454                 return dlm_posix_get(ls->ls_dlm, ip->i_no_addr, file, fl);
1455         else if (lock_is_unlock(fl))
1456                 return dlm_posix_unlock(ls->ls_dlm, ip->i_no_addr, file, fl);
1457         else
1458                 return dlm_posix_lock(ls->ls_dlm, ip->i_no_addr, file, cmd, fl);
1459 }
1460
1461 static void __flock_holder_uninit(struct file *file, struct gfs2_holder *fl_gh)
1462 {
1463         struct gfs2_glock *gl = gfs2_glock_hold(fl_gh->gh_gl);
1464
1465         /*
1466          * Make sure gfs2_glock_put() won't sleep under the file->f_lock
1467          * spinlock.
1468          */
1469
1470         spin_lock(&file->f_lock);
1471         gfs2_holder_uninit(fl_gh);
1472         spin_unlock(&file->f_lock);
1473         gfs2_glock_put(gl);
1474 }
1475
1476 static int do_flock(struct file *file, int cmd, struct file_lock *fl)
1477 {
1478         struct gfs2_file *fp = file->private_data;
1479         struct gfs2_holder *fl_gh = &fp->f_fl_gh;
1480         struct gfs2_inode *ip = GFS2_I(file_inode(file));
1481         struct gfs2_glock *gl;
1482         unsigned int state;
1483         u16 flags;
1484         int error = 0;
1485         int sleeptime;
1486
1487         state = lock_is_write(fl) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
1488         flags = GL_EXACT | GL_NOPID;
1489         if (!IS_SETLKW(cmd))
1490                 flags |= LM_FLAG_TRY_1CB;
1491
1492         mutex_lock(&fp->f_fl_mutex);
1493
1494         if (gfs2_holder_initialized(fl_gh)) {
1495                 struct file_lock request;
1496                 if (fl_gh->gh_state == state)
1497                         goto out;
1498                 locks_init_lock(&request);
1499                 request.c.flc_type = F_UNLCK;
1500                 request.c.flc_flags = FL_FLOCK;
1501                 locks_lock_file_wait(file, &request);
1502                 gfs2_glock_dq(fl_gh);
1503                 gfs2_holder_reinit(state, flags, fl_gh);
1504         } else {
1505                 error = gfs2_glock_get(GFS2_SB(&ip->i_inode), ip->i_no_addr,
1506                                        &gfs2_flock_glops, CREATE, &gl);
1507                 if (error)
1508                         goto out;
1509                 spin_lock(&file->f_lock);
1510                 gfs2_holder_init(gl, state, flags, fl_gh);
1511                 spin_unlock(&file->f_lock);
1512                 gfs2_glock_put(gl);
1513         }
1514         for (sleeptime = 1; sleeptime <= 4; sleeptime <<= 1) {
1515                 error = gfs2_glock_nq(fl_gh);
1516                 if (error != GLR_TRYFAILED)
1517                         break;
1518                 fl_gh->gh_flags &= ~LM_FLAG_TRY_1CB;
1519                 fl_gh->gh_flags |= LM_FLAG_TRY;
1520                 msleep(sleeptime);
1521         }
1522         if (error) {
1523                 __flock_holder_uninit(file, fl_gh);
1524                 if (error == GLR_TRYFAILED)
1525                         error = -EAGAIN;
1526         } else {
1527                 error = locks_lock_file_wait(file, fl);
1528                 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
1529         }
1530
1531 out:
1532         mutex_unlock(&fp->f_fl_mutex);
1533         return error;
1534 }
1535
1536 static void do_unflock(struct file *file, struct file_lock *fl)
1537 {
1538         struct gfs2_file *fp = file->private_data;
1539         struct gfs2_holder *fl_gh = &fp->f_fl_gh;
1540
1541         mutex_lock(&fp->f_fl_mutex);
1542         locks_lock_file_wait(file, fl);
1543         if (gfs2_holder_initialized(fl_gh)) {
1544                 gfs2_glock_dq(fl_gh);
1545                 __flock_holder_uninit(file, fl_gh);
1546         }
1547         mutex_unlock(&fp->f_fl_mutex);
1548 }
1549
1550 /**
1551  * gfs2_flock - acquire/release a flock lock on a file
1552  * @file: the file pointer
1553  * @cmd: either modify or retrieve lock state, possibly wait
1554  * @fl: type and range of lock
1555  *
1556  * Returns: errno
1557  */
1558
1559 static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
1560 {
1561         if (!(fl->c.flc_flags & FL_FLOCK))
1562                 return -ENOLCK;
1563
1564         if (lock_is_unlock(fl)) {
1565                 do_unflock(file, fl);
1566                 return 0;
1567         } else {
1568                 return do_flock(file, cmd, fl);
1569         }
1570 }
1571
1572 const struct file_operations gfs2_file_fops = {
1573         .llseek         = gfs2_llseek,
1574         .read_iter      = gfs2_file_read_iter,
1575         .write_iter     = gfs2_file_write_iter,
1576         .iopoll         = iocb_bio_iopoll,
1577         .unlocked_ioctl = gfs2_ioctl,
1578         .compat_ioctl   = gfs2_compat_ioctl,
1579         .mmap           = gfs2_mmap,
1580         .open           = gfs2_open,
1581         .release        = gfs2_release,
1582         .fsync          = gfs2_fsync,
1583         .lock           = gfs2_lock,
1584         .flock          = gfs2_flock,
1585         .splice_read    = copy_splice_read,
1586         .splice_write   = gfs2_file_splice_write,
1587         .setlease       = simple_nosetlease,
1588         .fallocate      = gfs2_fallocate,
1589 };
1590
1591 const struct file_operations gfs2_dir_fops = {
1592         .iterate_shared = gfs2_readdir,
1593         .unlocked_ioctl = gfs2_ioctl,
1594         .compat_ioctl   = gfs2_compat_ioctl,
1595         .open           = gfs2_open,
1596         .release        = gfs2_release,
1597         .fsync          = gfs2_fsync,
1598         .lock           = gfs2_lock,
1599         .flock          = gfs2_flock,
1600         .llseek         = default_llseek,
1601 };
1602
1603 #endif /* CONFIG_GFS2_FS_LOCKING_DLM */
1604
1605 const struct file_operations gfs2_file_fops_nolock = {
1606         .llseek         = gfs2_llseek,
1607         .read_iter      = gfs2_file_read_iter,
1608         .write_iter     = gfs2_file_write_iter,
1609         .iopoll         = iocb_bio_iopoll,
1610         .unlocked_ioctl = gfs2_ioctl,
1611         .compat_ioctl   = gfs2_compat_ioctl,
1612         .mmap           = gfs2_mmap,
1613         .open           = gfs2_open,
1614         .release        = gfs2_release,
1615         .fsync          = gfs2_fsync,
1616         .splice_read    = copy_splice_read,
1617         .splice_write   = gfs2_file_splice_write,
1618         .setlease       = generic_setlease,
1619         .fallocate      = gfs2_fallocate,
1620 };
1621
1622 const struct file_operations gfs2_dir_fops_nolock = {
1623         .iterate_shared = gfs2_readdir,
1624         .unlocked_ioctl = gfs2_ioctl,
1625         .compat_ioctl   = gfs2_compat_ioctl,
1626         .open           = gfs2_open,
1627         .release        = gfs2_release,
1628         .fsync          = gfs2_fsync,
1629         .llseek         = default_llseek,
1630 };
1631
This page took 0.129359 seconds and 4 git commands to generate.