linux/fs/gfs2/file.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
   3 * Copyright (C) 2004-2006 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 version 2.
   8 */
   9
  10#include <linux/slab.h>
  11#include <linux/spinlock.h>
  12#include <linux/completion.h>
  13#include <linux/buffer_head.h>
  14#include <linux/pagemap.h>
  15#include <linux/uio.h>
  16#include <linux/blkdev.h>
  17#include <linux/mm.h>
  18#include <linux/mount.h>
  19#include <linux/fs.h>
  20#include <linux/gfs2_ondisk.h>
  21#include <linux/falloc.h>
  22#include <linux/swap.h>
  23#include <linux/crc32.h>
  24#include <linux/writeback.h>
  25#include <linux/uaccess.h>
  26#include <linux/dlm.h>
  27#include <linux/dlm_plock.h>
  28#include <linux/delay.h>
  29#include <linux/backing-dev.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
  58static 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
 105static 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 * 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 */
 129static 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
 142static 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
 158static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
 159{
 160        struct inode *inode = file_inode(filp);
 161        struct gfs2_inode *ip = GFS2_I(inode);
 162        struct gfs2_holder gh;
 163        int error;
 164        u32 fsflags;
 165
 166        gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
 167        error = gfs2_glock_nq(&gh);
 168        if (error)
 169                goto out_uninit;
 170
 171        fsflags = gfs2_gfsflags_to_fsflags(inode, ip->i_diskflags);
 172
 173        if (put_user(fsflags, ptr))
 174                error = -EFAULT;
 175
 176        gfs2_glock_dq(&gh);
 177out_uninit:
 178        gfs2_holder_uninit(&gh);
 179        return error;
 180}
 181
 182void gfs2_set_inode_flags(struct inode *inode)
 183{
 184        struct gfs2_inode *ip = GFS2_I(inode);
 185        unsigned int flags = inode->i_flags;
 186
 187        flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_NOSEC);
 188        if ((ip->i_eattr == 0) && !is_sxid(inode->i_mode))
 189                flags |= S_NOSEC;
 190        if (ip->i_diskflags & GFS2_DIF_IMMUTABLE)
 191                flags |= S_IMMUTABLE;
 192        if (ip->i_diskflags & GFS2_DIF_APPENDONLY)
 193                flags |= S_APPEND;
 194        if (ip->i_diskflags & GFS2_DIF_NOATIME)
 195                flags |= S_NOATIME;
 196        if (ip->i_diskflags & GFS2_DIF_SYNC)
 197                flags |= S_SYNC;
 198        inode->i_flags = flags;
 199}
 200
 201/* Flags that can be set by user space */
 202#define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA|                    \
 203                             GFS2_DIF_IMMUTABLE|                \
 204                             GFS2_DIF_APPENDONLY|               \
 205                             GFS2_DIF_NOATIME|                  \
 206                             GFS2_DIF_SYNC|                     \
 207                             GFS2_DIF_TOPDIR|                   \
 208                             GFS2_DIF_INHERIT_JDATA)
 209
 210/**
 211 * do_gfs2_set_flags - set flags on an inode
 212 * @filp: file pointer
 213 * @reqflags: The flags to set
 214 * @mask: Indicates which flags are valid
 215 * @fsflags: The FS_* inode flags passed in
 216 *
 217 */
 218static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask,
 219                             const u32 fsflags)
 220{
 221        struct inode *inode = file_inode(filp);
 222        struct gfs2_inode *ip = GFS2_I(inode);
 223        struct gfs2_sbd *sdp = GFS2_SB(inode);
 224        struct buffer_head *bh;
 225        struct gfs2_holder gh;
 226        int error;
 227        u32 new_flags, flags, oldflags;
 228
 229        error = mnt_want_write_file(filp);
 230        if (error)
 231                return error;
 232
 233        error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
 234        if (error)
 235                goto out_drop_write;
 236
 237        oldflags = gfs2_gfsflags_to_fsflags(inode, ip->i_diskflags);
 238        error = vfs_ioc_setflags_prepare(inode, oldflags, fsflags);
 239        if (error)
 240                goto out;
 241
 242        error = -EACCES;
 243        if (!inode_owner_or_capable(inode))
 244                goto out;
 245
 246        error = 0;
 247        flags = ip->i_diskflags;
 248        new_flags = (flags & ~mask) | (reqflags & mask);
 249        if ((new_flags ^ flags) == 0)
 250                goto out;
 251
 252        error = -EPERM;
 253        if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
 254                goto out;
 255        if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
 256                goto out;
 257        if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
 258            !capable(CAP_LINUX_IMMUTABLE))
 259                goto out;
 260        if (!IS_IMMUTABLE(inode)) {
 261                error = gfs2_permission(inode, MAY_WRITE);
 262                if (error)
 263                        goto out;
 264        }
 265        if ((flags ^ new_flags) & GFS2_DIF_JDATA) {
 266                if (new_flags & GFS2_DIF_JDATA)
 267                        gfs2_log_flush(sdp, ip->i_gl,
 268                                       GFS2_LOG_HEAD_FLUSH_NORMAL |
 269                                       GFS2_LFC_SET_FLAGS);
 270                error = filemap_fdatawrite(inode->i_mapping);
 271                if (error)
 272                        goto out;
 273                error = filemap_fdatawait(inode->i_mapping);
 274                if (error)
 275                        goto out;
 276                if (new_flags & GFS2_DIF_JDATA)
 277                        gfs2_ordered_del_inode(ip);
 278        }
 279        error = gfs2_trans_begin(sdp, RES_DINODE, 0);
 280        if (error)
 281                goto out;
 282        error = gfs2_meta_inode_buffer(ip, &bh);
 283        if (error)
 284                goto out_trans_end;
 285        inode->i_ctime = current_time(inode);
 286        gfs2_trans_add_meta(ip->i_gl, bh);
 287        ip->i_diskflags = new_flags;
 288        gfs2_dinode_out(ip, bh->b_data);
 289        brelse(bh);
 290        gfs2_set_inode_flags(inode);
 291        gfs2_set_aops(inode);
 292out_trans_end:
 293        gfs2_trans_end(sdp);
 294out:
 295        gfs2_glock_dq_uninit(&gh);
 296out_drop_write:
 297        mnt_drop_write_file(filp);
 298        return error;
 299}
 300
 301static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
 302{
 303        struct inode *inode = file_inode(filp);
 304        u32 fsflags, gfsflags = 0;
 305        u32 mask;
 306        int i;
 307
 308        if (get_user(fsflags, ptr))
 309                return -EFAULT;
 310
 311        for (i = 0; i < ARRAY_SIZE(fsflag_gfs2flag); i++) {
 312                if (fsflags & fsflag_gfs2flag[i].fsflag) {
 313                        fsflags &= ~fsflag_gfs2flag[i].fsflag;
 314                        gfsflags |= fsflag_gfs2flag[i].gfsflag;
 315                }
 316        }
 317        if (fsflags || gfsflags & ~GFS2_FLAGS_USER_SET)
 318                return -EINVAL;
 319
 320        mask = GFS2_FLAGS_USER_SET;
 321        if (S_ISDIR(inode->i_mode)) {
 322                mask &= ~GFS2_DIF_JDATA;
 323        } else {
 324                /* The GFS2_DIF_TOPDIR flag is only valid for directories. */
 325                if (gfsflags & GFS2_DIF_TOPDIR)
 326                        return -EINVAL;
 327                mask &= ~(GFS2_DIF_TOPDIR | GFS2_DIF_INHERIT_JDATA);
 328        }
 329
 330        return do_gfs2_set_flags(filp, gfsflags, mask, fsflags);
 331}
 332
 333static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 334{
 335        switch(cmd) {
 336        case FS_IOC_GETFLAGS:
 337                return gfs2_get_flags(filp, (u32 __user *)arg);
 338        case FS_IOC_SETFLAGS:
 339                return gfs2_set_flags(filp, (u32 __user *)arg);
 340        case FITRIM:
 341                return gfs2_fitrim(filp, (void __user *)arg);
 342        }
 343        return -ENOTTY;
 344}
 345
 346/**
 347 * gfs2_size_hint - Give a hint to the size of a write request
 348 * @filep: The struct file
 349 * @offset: The file offset of the write
 350 * @size: The length of the write
 351 *
 352 * When we are about to do a write, this function records the total
 353 * write size in order to provide a suitable hint to the lower layers
 354 * about how many blocks will be required.
 355 *
 356 */
 357
 358static void gfs2_size_hint(struct file *filep, loff_t offset, size_t size)
 359{
 360        struct inode *inode = file_inode(filep);
 361        struct gfs2_sbd *sdp = GFS2_SB(inode);
 362        struct gfs2_inode *ip = GFS2_I(inode);
 363        size_t blks = (size + sdp->sd_sb.sb_bsize - 1) >> sdp->sd_sb.sb_bsize_shift;
 364        int hint = min_t(size_t, INT_MAX, blks);
 365
 366        if (hint > atomic_read(&ip->i_sizehint))
 367                atomic_set(&ip->i_sizehint, hint);
 368}
 369
 370/**
 371 * gfs2_allocate_page_backing - Allocate blocks for a write fault
 372 * @page: The (locked) page to allocate backing for
 373 * @length: Size of the allocation
 374 *
 375 * We try to allocate all the blocks required for the page in one go.  This
 376 * might fail for various reasons, so we keep trying until all the blocks to
 377 * back this page are allocated.  If some of the blocks are already allocated,
 378 * that is ok too.
 379 */
 380static int gfs2_allocate_page_backing(struct page *page, unsigned int length)
 381{
 382        u64 pos = page_offset(page);
 383
 384        do {
 385                struct iomap iomap = { };
 386
 387                if (gfs2_iomap_alloc(page->mapping->host, pos, length, &iomap))
 388                        return -EIO;
 389
 390                if (length < iomap.length)
 391                        iomap.length = length;
 392                length -= iomap.length;
 393                pos += iomap.length;
 394        } while (length > 0);
 395
 396        return 0;
 397}
 398
 399/**
 400 * gfs2_page_mkwrite - Make a shared, mmap()ed, page writable
 401 * @vma: The virtual memory area
 402 * @vmf: The virtual memory fault containing the page to become writable
 403 *
 404 * When the page becomes writable, we need to ensure that we have
 405 * blocks allocated on disk to back that page.
 406 */
 407
 408static vm_fault_t gfs2_page_mkwrite(struct vm_fault *vmf)
 409{
 410        struct page *page = vmf->page;
 411        struct inode *inode = file_inode(vmf->vma->vm_file);
 412        struct gfs2_inode *ip = GFS2_I(inode);
 413        struct gfs2_sbd *sdp = GFS2_SB(inode);
 414        struct gfs2_alloc_parms ap = { .aflags = 0, };
 415        u64 offset = page_offset(page);
 416        unsigned int data_blocks, ind_blocks, rblocks;
 417        vm_fault_t ret = VM_FAULT_LOCKED;
 418        struct gfs2_holder gh;
 419        unsigned int length;
 420        loff_t size;
 421        int err;
 422
 423        sb_start_pagefault(inode->i_sb);
 424
 425        gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
 426        err = gfs2_glock_nq(&gh);
 427        if (err) {
 428                ret = block_page_mkwrite_return(err);
 429                goto out_uninit;
 430        }
 431
 432        /* Check page index against inode size */
 433        size = i_size_read(inode);
 434        if (offset >= size) {
 435                ret = VM_FAULT_SIGBUS;
 436                goto out_unlock;
 437        }
 438
 439        /* Update file times before taking page lock */
 440        file_update_time(vmf->vma->vm_file);
 441
 442        /* page is wholly or partially inside EOF */
 443        if (size - offset < PAGE_SIZE)
 444                length = size - offset;
 445        else
 446                length = PAGE_SIZE;
 447
 448        gfs2_size_hint(vmf->vma->vm_file, offset, length);
 449
 450        set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
 451        set_bit(GIF_SW_PAGED, &ip->i_flags);
 452
 453        /*
 454         * iomap_writepage / iomap_writepages currently don't support inline
 455         * files, so always unstuff here.
 456         */
 457
 458        if (!gfs2_is_stuffed(ip) &&
 459            !gfs2_write_alloc_required(ip, offset, length)) {
 460                lock_page(page);
 461                if (!PageUptodate(page) || page->mapping != inode->i_mapping) {
 462                        ret = VM_FAULT_NOPAGE;
 463                        unlock_page(page);
 464                }
 465                goto out_unlock;
 466        }
 467
 468        err = gfs2_rindex_update(sdp);
 469        if (err) {
 470                ret = block_page_mkwrite_return(err);
 471                goto out_unlock;
 472        }
 473
 474        gfs2_write_calc_reserv(ip, length, &data_blocks, &ind_blocks);
 475        ap.target = data_blocks + ind_blocks;
 476        err = gfs2_quota_lock_check(ip, &ap);
 477        if (err) {
 478                ret = block_page_mkwrite_return(err);
 479                goto out_unlock;
 480        }
 481        err = gfs2_inplace_reserve(ip, &ap);
 482        if (err) {
 483                ret = block_page_mkwrite_return(err);
 484                goto out_quota_unlock;
 485        }
 486
 487        rblocks = RES_DINODE + ind_blocks;
 488        if (gfs2_is_jdata(ip))
 489                rblocks += data_blocks ? data_blocks : 1;
 490        if (ind_blocks || data_blocks) {
 491                rblocks += RES_STATFS + RES_QUOTA;
 492                rblocks += gfs2_rg_blocks(ip, data_blocks + ind_blocks);
 493        }
 494        err = gfs2_trans_begin(sdp, rblocks, 0);
 495        if (err) {
 496                ret = block_page_mkwrite_return(err);
 497                goto out_trans_fail;
 498        }
 499
 500        /* Unstuff, if required, and allocate backing blocks for page */
 501        if (gfs2_is_stuffed(ip)) {
 502                err = gfs2_unstuff_dinode(ip);
 503                if (err) {
 504                        ret = block_page_mkwrite_return(err);
 505                        goto out_trans_end;
 506                }
 507        }
 508
 509        lock_page(page);
 510        /* If truncated, we must retry the operation, we may have raced
 511         * with the glock demotion code.
 512         */
 513        if (!PageUptodate(page) || page->mapping != inode->i_mapping) {
 514                ret = VM_FAULT_NOPAGE;
 515                goto out_page_locked;
 516        }
 517
 518        err = gfs2_allocate_page_backing(page, length);
 519        if (err)
 520                ret = block_page_mkwrite_return(err);
 521
 522out_page_locked:
 523        if (ret != VM_FAULT_LOCKED)
 524                unlock_page(page);
 525out_trans_end:
 526        gfs2_trans_end(sdp);
 527out_trans_fail:
 528        gfs2_inplace_release(ip);
 529out_quota_unlock:
 530        gfs2_quota_unlock(ip);
 531out_unlock:
 532        gfs2_glock_dq(&gh);
 533out_uninit:
 534        gfs2_holder_uninit(&gh);
 535        if (ret == VM_FAULT_LOCKED) {
 536                set_page_dirty(page);
 537                wait_for_stable_page(page);
 538        }
 539        sb_end_pagefault(inode->i_sb);
 540        return ret;
 541}
 542
 543static vm_fault_t gfs2_fault(struct vm_fault *vmf)
 544{
 545        struct inode *inode = file_inode(vmf->vma->vm_file);
 546        struct gfs2_inode *ip = GFS2_I(inode);
 547        struct gfs2_holder gh;
 548        vm_fault_t ret;
 549        int err;
 550
 551        gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
 552        err = gfs2_glock_nq(&gh);
 553        if (err) {
 554                ret = block_page_mkwrite_return(err);
 555                goto out_uninit;
 556        }
 557        ret = filemap_fault(vmf);
 558        gfs2_glock_dq(&gh);
 559out_uninit:
 560        gfs2_holder_uninit(&gh);
 561        return ret;
 562}
 563
 564static const struct vm_operations_struct gfs2_vm_ops = {
 565        .fault = gfs2_fault,
 566        .map_pages = filemap_map_pages,
 567        .page_mkwrite = gfs2_page_mkwrite,
 568};
 569
 570/**
 571 * gfs2_mmap -
 572 * @file: The file to map
 573 * @vma: The VMA which described the mapping
 574 *
 575 * There is no need to get a lock here unless we should be updating
 576 * atime. We ignore any locking errors since the only consequence is
 577 * a missed atime update (which will just be deferred until later).
 578 *
 579 * Returns: 0
 580 */
 581
 582static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
 583{
 584        struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
 585
 586        if (!(file->f_flags & O_NOATIME) &&
 587            !IS_NOATIME(&ip->i_inode)) {
 588                struct gfs2_holder i_gh;
 589                int error;
 590
 591                error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
 592                                           &i_gh);
 593                if (error)
 594                        return error;
 595                /* grab lock to update inode */
 596                gfs2_glock_dq_uninit(&i_gh);
 597                file_accessed(file);
 598        }
 599        vma->vm_ops = &gfs2_vm_ops;
 600
 601        return 0;
 602}
 603
 604/**
 605 * gfs2_open_common - This is common to open and atomic_open
 606 * @inode: The inode being opened
 607 * @file: The file being opened
 608 *
 609 * This maybe called under a glock or not depending upon how it has
 610 * been called. We must always be called under a glock for regular
 611 * files, however. For other file types, it does not matter whether
 612 * we hold the glock or not.
 613 *
 614 * Returns: Error code or 0 for success
 615 */
 616
 617int gfs2_open_common(struct inode *inode, struct file *file)
 618{
 619        struct gfs2_file *fp;
 620        int ret;
 621
 622        if (S_ISREG(inode->i_mode)) {
 623                ret = generic_file_open(inode, file);
 624                if (ret)
 625                        return ret;
 626        }
 627
 628        fp = kzalloc(sizeof(struct gfs2_file), GFP_NOFS);
 629        if (!fp)
 630                return -ENOMEM;
 631
 632        mutex_init(&fp->f_fl_mutex);
 633
 634        gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
 635        file->private_data = fp;
 636        if (file->f_mode & FMODE_WRITE) {
 637                ret = gfs2_qa_get(GFS2_I(inode));
 638                if (ret)
 639                        goto fail;
 640        }
 641        return 0;
 642
 643fail:
 644        kfree(file->private_data);
 645        file->private_data = NULL;
 646        return ret;
 647}
 648
 649/**
 650 * gfs2_open - open a file
 651 * @inode: the inode to open
 652 * @file: the struct file for this opening
 653 *
 654 * After atomic_open, this function is only used for opening files
 655 * which are already cached. We must still get the glock for regular
 656 * files to ensure that we have the file size uptodate for the large
 657 * file check which is in the common code. That is only an issue for
 658 * regular files though.
 659 *
 660 * Returns: errno
 661 */
 662
 663static int gfs2_open(struct inode *inode, struct file *file)
 664{
 665        struct gfs2_inode *ip = GFS2_I(inode);
 666        struct gfs2_holder i_gh;
 667        int error;
 668        bool need_unlock = false;
 669
 670        if (S_ISREG(ip->i_inode.i_mode)) {
 671                error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
 672                                           &i_gh);
 673                if (error)
 674                        return error;
 675                need_unlock = true;
 676        }
 677
 678        error = gfs2_open_common(inode, file);
 679
 680        if (need_unlock)
 681                gfs2_glock_dq_uninit(&i_gh);
 682
 683        return error;
 684}
 685
 686/**
 687 * gfs2_release - called to close a struct file
 688 * @inode: the inode the struct file belongs to
 689 * @file: the struct file being closed
 690 *
 691 * Returns: errno
 692 */
 693
 694static int gfs2_release(struct inode *inode, struct file *file)
 695{
 696        struct gfs2_inode *ip = GFS2_I(inode);
 697
 698        kfree(file->private_data);
 699        file->private_data = NULL;
 700
 701        if (gfs2_rs_active(&ip->i_res))
 702                gfs2_rs_delete(ip, &inode->i_writecount);
 703        if (file->f_mode & FMODE_WRITE)
 704                gfs2_qa_put(ip);
 705        return 0;
 706}
 707
 708/**
 709 * gfs2_fsync - sync the dirty data for a file (across the cluster)
 710 * @file: the file that points to the dentry
 711 * @start: the start position in the file to sync
 712 * @end: the end position in the file to sync
 713 * @datasync: set if we can ignore timestamp changes
 714 *
 715 * We split the data flushing here so that we don't wait for the data
 716 * until after we've also sent the metadata to disk. Note that for
 717 * data=ordered, we will write & wait for the data at the log flush
 718 * stage anyway, so this is unlikely to make much of a difference
 719 * except in the data=writeback case.
 720 *
 721 * If the fdatawrite fails due to any reason except -EIO, we will
 722 * continue the remainder of the fsync, although we'll still report
 723 * the error at the end. This is to match filemap_write_and_wait_range()
 724 * behaviour.
 725 *
 726 * Returns: errno
 727 */
 728
 729static int gfs2_fsync(struct file *file, loff_t start, loff_t end,
 730                      int datasync)
 731{
 732        struct address_space *mapping = file->f_mapping;
 733        struct inode *inode = mapping->host;
 734        int sync_state = inode->i_state & I_DIRTY_ALL;
 735        struct gfs2_inode *ip = GFS2_I(inode);
 736        int ret = 0, ret1 = 0;
 737
 738        if (mapping->nrpages) {
 739                ret1 = filemap_fdatawrite_range(mapping, start, end);
 740                if (ret1 == -EIO)
 741                        return ret1;
 742        }
 743
 744        if (!gfs2_is_jdata(ip))
 745                sync_state &= ~I_DIRTY_PAGES;
 746        if (datasync)
 747                sync_state &= ~(I_DIRTY_SYNC | I_DIRTY_TIME);
 748
 749        if (sync_state) {
 750                ret = sync_inode_metadata(inode, 1);
 751                if (ret)
 752                        return ret;
 753                if (gfs2_is_jdata(ip))
 754                        ret = file_write_and_wait(file);
 755                if (ret)
 756                        return ret;
 757                gfs2_ail_flush(ip->i_gl, 1);
 758        }
 759
 760        if (mapping->nrpages)
 761                ret = file_fdatawait_range(file, start, end);
 762
 763        return ret ? ret : ret1;
 764}
 765
 766static ssize_t gfs2_file_direct_read(struct kiocb *iocb, struct iov_iter *to,
 767                                     struct gfs2_holder *gh)
 768{
 769        struct file *file = iocb->ki_filp;
 770        struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
 771        size_t count = iov_iter_count(to);
 772        ssize_t ret;
 773
 774        if (!count)
 775                return 0; /* skip atime */
 776
 777        gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, 0, gh);
 778        ret = gfs2_glock_nq(gh);
 779        if (ret)
 780                goto out_uninit;
 781
 782        ret = iomap_dio_rw(iocb, to, &gfs2_iomap_ops, NULL, 0);
 783        gfs2_glock_dq(gh);
 784out_uninit:
 785        gfs2_holder_uninit(gh);
 786        return ret;
 787}
 788
 789static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from,
 790                                      struct gfs2_holder *gh)
 791{
 792        struct file *file = iocb->ki_filp;
 793        struct inode *inode = file->f_mapping->host;
 794        struct gfs2_inode *ip = GFS2_I(inode);
 795        size_t len = iov_iter_count(from);
 796        loff_t offset = iocb->ki_pos;
 797        ssize_t ret;
 798
 799        /*
 800         * Deferred lock, even if its a write, since we do no allocation on
 801         * this path. All we need to change is the atime, and this lock mode
 802         * ensures that other nodes have flushed their buffered read caches
 803         * (i.e. their page cache entries for this inode). We do not,
 804         * unfortunately, have the option of only flushing a range like the
 805         * VFS does.
 806         */
 807        gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, 0, gh);
 808        ret = gfs2_glock_nq(gh);
 809        if (ret)
 810                goto out_uninit;
 811
 812        /* Silently fall back to buffered I/O when writing beyond EOF */
 813        if (offset + len > i_size_read(&ip->i_inode))
 814                goto out;
 815
 816        ret = iomap_dio_rw(iocb, from, &gfs2_iomap_ops, NULL, 0);
 817        if (ret == -ENOTBLK)
 818                ret = 0;
 819out:
 820        gfs2_glock_dq(gh);
 821out_uninit:
 822        gfs2_holder_uninit(gh);
 823        return ret;
 824}
 825
 826static ssize_t gfs2_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
 827{
 828        struct gfs2_inode *ip;
 829        struct gfs2_holder gh;
 830        size_t written = 0;
 831        ssize_t ret;
 832
 833        if (iocb->ki_flags & IOCB_DIRECT) {
 834                ret = gfs2_file_direct_read(iocb, to, &gh);
 835                if (likely(ret != -ENOTBLK))
 836                        return ret;
 837                iocb->ki_flags &= ~IOCB_DIRECT;
 838        }
 839        iocb->ki_flags |= IOCB_NOIO;
 840        ret = generic_file_read_iter(iocb, to);
 841        iocb->ki_flags &= ~IOCB_NOIO;
 842        if (ret >= 0) {
 843                if (!iov_iter_count(to))
 844                        return ret;
 845                written = ret;
 846        } else {
 847                if (ret != -EAGAIN)
 848                        return ret;
 849                if (iocb->ki_flags & IOCB_NOWAIT)
 850                        return ret;
 851        }
 852        ip = GFS2_I(iocb->ki_filp->f_mapping->host);
 853        gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
 854        ret = gfs2_glock_nq(&gh);
 855        if (ret)
 856                goto out_uninit;
 857        ret = generic_file_read_iter(iocb, to);
 858        if (ret > 0)
 859                written += ret;
 860        gfs2_glock_dq(&gh);
 861out_uninit:
 862        gfs2_holder_uninit(&gh);
 863        return written ? written : ret;
 864}
 865
 866/**
 867 * gfs2_file_write_iter - Perform a write to a file
 868 * @iocb: The io context
 869 * @from: The data to write
 870 *
 871 * We have to do a lock/unlock here to refresh the inode size for
 872 * O_APPEND writes, otherwise we can land up writing at the wrong
 873 * offset. There is still a race, but provided the app is using its
 874 * own file locking, this will make O_APPEND work as expected.
 875 *
 876 */
 877
 878static ssize_t gfs2_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
 879{
 880        struct file *file = iocb->ki_filp;
 881        struct inode *inode = file_inode(file);
 882        struct gfs2_inode *ip = GFS2_I(inode);
 883        struct gfs2_holder gh;
 884        ssize_t ret;
 885
 886        gfs2_size_hint(file, iocb->ki_pos, iov_iter_count(from));
 887
 888        if (iocb->ki_flags & IOCB_APPEND) {
 889                ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
 890                if (ret)
 891                        return ret;
 892                gfs2_glock_dq_uninit(&gh);
 893        }
 894
 895        inode_lock(inode);
 896        ret = generic_write_checks(iocb, from);
 897        if (ret <= 0)
 898                goto out_unlock;
 899
 900        ret = file_remove_privs(file);
 901        if (ret)
 902                goto out_unlock;
 903
 904        ret = file_update_time(file);
 905        if (ret)
 906                goto out_unlock;
 907
 908        if (iocb->ki_flags & IOCB_DIRECT) {
 909                struct address_space *mapping = file->f_mapping;
 910                ssize_t buffered, ret2;
 911
 912                ret = gfs2_file_direct_write(iocb, from, &gh);
 913                if (ret < 0 || !iov_iter_count(from))
 914                        goto out_unlock;
 915
 916                iocb->ki_flags |= IOCB_DSYNC;
 917                current->backing_dev_info = inode_to_bdi(inode);
 918                buffered = iomap_file_buffered_write(iocb, from, &gfs2_iomap_ops);
 919                current->backing_dev_info = NULL;
 920                if (unlikely(buffered <= 0)) {
 921                        if (!ret)
 922                                ret = buffered;
 923                        goto out_unlock;
 924                }
 925
 926                /*
 927                 * We need to ensure that the page cache pages are written to
 928                 * disk and invalidated to preserve the expected O_DIRECT
 929                 * semantics.  If the writeback or invalidate fails, only report
 930                 * the direct I/O range as we don't know if the buffered pages
 931                 * made it to disk.
 932                 */
 933                iocb->ki_pos += buffered;
 934                ret2 = generic_write_sync(iocb, buffered);
 935                invalidate_mapping_pages(mapping,
 936                                (iocb->ki_pos - buffered) >> PAGE_SHIFT,
 937                                (iocb->ki_pos - 1) >> PAGE_SHIFT);
 938                if (!ret || ret2 > 0)
 939                        ret += ret2;
 940        } else {
 941                current->backing_dev_info = inode_to_bdi(inode);
 942                ret = iomap_file_buffered_write(iocb, from, &gfs2_iomap_ops);
 943                current->backing_dev_info = NULL;
 944                if (likely(ret > 0)) {
 945                        iocb->ki_pos += ret;
 946                        ret = generic_write_sync(iocb, ret);
 947                }
 948        }
 949
 950out_unlock:
 951        inode_unlock(inode);
 952        return ret;
 953}
 954
 955static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
 956                           int mode)
 957{
 958        struct super_block *sb = inode->i_sb;
 959        struct gfs2_inode *ip = GFS2_I(inode);
 960        loff_t end = offset + len;
 961        struct buffer_head *dibh;
 962        int error;
 963
 964        error = gfs2_meta_inode_buffer(ip, &dibh);
 965        if (unlikely(error))
 966                return error;
 967
 968        gfs2_trans_add_meta(ip->i_gl, dibh);
 969
 970        if (gfs2_is_stuffed(ip)) {
 971                error = gfs2_unstuff_dinode(ip);
 972                if (unlikely(error))
 973                        goto out;
 974        }
 975
 976        while (offset < end) {
 977                struct iomap iomap = { };
 978
 979                error = gfs2_iomap_alloc(inode, offset, end - offset, &iomap);
 980                if (error)
 981                        goto out;
 982                offset = iomap.offset + iomap.length;
 983                if (!(iomap.flags & IOMAP_F_NEW))
 984                        continue;
 985                error = sb_issue_zeroout(sb, iomap.addr >> inode->i_blkbits,
 986                                         iomap.length >> inode->i_blkbits,
 987                                         GFP_NOFS);
 988                if (error) {
 989                        fs_err(GFS2_SB(inode), "Failed to zero data buffers\n");
 990                        goto out;
 991                }
 992        }
 993out:
 994        brelse(dibh);
 995        return error;
 996}
 997/**
 998 * calc_max_reserv() - Reverse of write_calc_reserv. Given a number of
 999 *                     blocks, determine how many bytes can be written.
1000 * @ip:          The inode in question.
1001 * @len:         Max cap of bytes. What we return in *len must be <= this.
1002 * @data_blocks: Compute and return the number of data blocks needed
1003 * @ind_blocks:  Compute and return the number of indirect blocks needed
1004 * @max_blocks:  The total blocks available to work with.
1005 *
1006 * Returns: void, but @len, @data_blocks and @ind_blocks are filled in.
1007 */
1008static void calc_max_reserv(struct gfs2_inode *ip, loff_t *len,
1009                            unsigned int *data_blocks, unsigned int *ind_blocks,
1010                            unsigned int max_blocks)
1011{
1012        loff_t max = *len;
1013        const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1014        unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
1015
1016        for (tmp = max_data; tmp > sdp->sd_diptrs;) {
1017                tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
1018                max_data -= tmp;
1019        }
1020
1021        *data_blocks = max_data;
1022        *ind_blocks = max_blocks - max_data;
1023        *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
1024        if (*len > max) {
1025                *len = max;
1026                gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
1027        }
1028}
1029
1030static long __gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
1031{
1032        struct inode *inode = file_inode(file);
1033        struct gfs2_sbd *sdp = GFS2_SB(inode);
1034        struct gfs2_inode *ip = GFS2_I(inode);
1035        struct gfs2_alloc_parms ap = { .aflags = 0, };
1036        unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
1037        loff_t bytes, max_bytes, max_blks;
1038        int error;
1039        const loff_t pos = offset;
1040        const loff_t count = len;
1041        loff_t bsize_mask = ~((loff_t)sdp->sd_sb.sb_bsize - 1);
1042        loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
1043        loff_t max_chunk_size = UINT_MAX & bsize_mask;
1044
1045        next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
1046
1047        offset &= bsize_mask;
1048
1049        len = next - offset;
1050        bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
1051        if (!bytes)
1052                bytes = UINT_MAX;
1053        bytes &= bsize_mask;
1054        if (bytes == 0)
1055                bytes = sdp->sd_sb.sb_bsize;
1056
1057        gfs2_size_hint(file, offset, len);
1058
1059        gfs2_write_calc_reserv(ip, PAGE_SIZE, &data_blocks, &ind_blocks);
1060        ap.min_target = data_blocks + ind_blocks;
1061
1062        while (len > 0) {
1063                if (len < bytes)
1064                        bytes = len;
1065                if (!gfs2_write_alloc_required(ip, offset, bytes)) {
1066                        len -= bytes;
1067                        offset += bytes;
1068                        continue;
1069                }
1070
1071                /* We need to determine how many bytes we can actually
1072                 * fallocate without exceeding quota or going over the
1073                 * end of the fs. We start off optimistically by assuming
1074                 * we can write max_bytes */
1075                max_bytes = (len > max_chunk_size) ? max_chunk_size : len;
1076
1077                /* Since max_bytes is most likely a theoretical max, we
1078                 * calculate a more realistic 'bytes' to serve as a good
1079                 * starting point for the number of bytes we may be able
1080                 * to write */
1081                gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
1082                ap.target = data_blocks + ind_blocks;
1083
1084                error = gfs2_quota_lock_check(ip, &ap);
1085                if (error)
1086                        return error;
1087                /* ap.allowed tells us how many blocks quota will allow
1088                 * us to write. Check if this reduces max_blks */
1089                max_blks = UINT_MAX;
1090                if (ap.allowed)
1091                        max_blks = ap.allowed;
1092
1093                error = gfs2_inplace_reserve(ip, &ap);
1094                if (error)
1095                        goto out_qunlock;
1096
1097                /* check if the selected rgrp limits our max_blks further */
1098                if (ip->i_res.rs_reserved < max_blks)
1099                        max_blks = ip->i_res.rs_reserved;
1100
1101                /* Almost done. Calculate bytes that can be written using
1102                 * max_blks. We also recompute max_bytes, data_blocks and
1103                 * ind_blocks */
1104                calc_max_reserv(ip, &max_bytes, &data_blocks,
1105                                &ind_blocks, max_blks);
1106
1107                rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
1108                          RES_RG_HDR + gfs2_rg_blocks(ip, data_blocks + ind_blocks);
1109                if (gfs2_is_jdata(ip))
1110                        rblocks += data_blocks ? data_blocks : 1;
1111
1112                error = gfs2_trans_begin(sdp, rblocks,
1113                                         PAGE_SIZE/sdp->sd_sb.sb_bsize);
1114                if (error)
1115                        goto out_trans_fail;
1116
1117                error = fallocate_chunk(inode, offset, max_bytes, mode);
1118                gfs2_trans_end(sdp);
1119
1120                if (error)
1121                        goto out_trans_fail;
1122
1123                len -= max_bytes;
1124                offset += max_bytes;
1125                gfs2_inplace_release(ip);
1126                gfs2_quota_unlock(ip);
1127        }
1128
1129        if (!(mode & FALLOC_FL_KEEP_SIZE) && (pos + count) > inode->i_size)
1130                i_size_write(inode, pos + count);
1131        file_update_time(file);
1132        mark_inode_dirty(inode);
1133
1134        if ((file->f_flags & O_DSYNC) || IS_SYNC(file->f_mapping->host))
1135                return vfs_fsync_range(file, pos, pos + count - 1,
1136                               (file->f_flags & __O_SYNC) ? 0 : 1);
1137        return 0;
1138
1139out_trans_fail:
1140        gfs2_inplace_release(ip);
1141out_qunlock:
1142        gfs2_quota_unlock(ip);
1143        return error;
1144}
1145
1146static long gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
1147{
1148        struct inode *inode = file_inode(file);
1149        struct gfs2_sbd *sdp = GFS2_SB(inode);
1150        struct gfs2_inode *ip = GFS2_I(inode);
1151        struct gfs2_holder gh;
1152        int ret;
1153
1154        if (mode & ~(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE))
1155                return -EOPNOTSUPP;
1156        /* fallocate is needed by gfs2_grow to reserve space in the rindex */
1157        if (gfs2_is_jdata(ip) && inode != sdp->sd_rindex)
1158                return -EOPNOTSUPP;
1159
1160        inode_lock(inode);
1161
1162        gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1163        ret = gfs2_glock_nq(&gh);
1164        if (ret)
1165                goto out_uninit;
1166
1167        if (!(mode & FALLOC_FL_KEEP_SIZE) &&
1168            (offset + len) > inode->i_size) {
1169                ret = inode_newsize_ok(inode, offset + len);
1170                if (ret)
1171                        goto out_unlock;
1172        }
1173
1174        ret = get_write_access(inode);
1175        if (ret)
1176                goto out_unlock;
1177
1178        if (mode & FALLOC_FL_PUNCH_HOLE) {
1179                ret = __gfs2_punch_hole(file, offset, len);
1180        } else {
1181                ret = __gfs2_fallocate(file, mode, offset, len);
1182                if (ret)
1183                        gfs2_rs_deltree(&ip->i_res);
1184        }
1185
1186        put_write_access(inode);
1187out_unlock:
1188        gfs2_glock_dq(&gh);
1189out_uninit:
1190        gfs2_holder_uninit(&gh);
1191        inode_unlock(inode);
1192        return ret;
1193}
1194
1195static ssize_t gfs2_file_splice_write(struct pipe_inode_info *pipe,
1196                                      struct file *out, loff_t *ppos,
1197                                      size_t len, unsigned int flags)
1198{
1199        ssize_t ret;
1200
1201        gfs2_size_hint(out, *ppos, len);
1202
1203        ret = iter_file_splice_write(pipe, out, ppos, len, flags);
1204        return ret;
1205}
1206
1207#ifdef CONFIG_GFS2_FS_LOCKING_DLM
1208
1209/**
1210 * gfs2_lock - acquire/release a posix lock on a file
1211 * @file: the file pointer
1212 * @cmd: either modify or retrieve lock state, possibly wait
1213 * @fl: type and range of lock
1214 *
1215 * Returns: errno
1216 */
1217
1218static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
1219{
1220        struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
1221        struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
1222        struct lm_lockstruct *ls = &sdp->sd_lockstruct;
1223
1224        if (!(fl->fl_flags & FL_POSIX))
1225                return -ENOLCK;
1226        if (__mandatory_lock(&ip->i_inode) && fl->fl_type != F_UNLCK)
1227                return -ENOLCK;
1228
1229        if (cmd == F_CANCELLK) {
1230                /* Hack: */
1231                cmd = F_SETLK;
1232                fl->fl_type = F_UNLCK;
1233        }
1234        if (unlikely(gfs2_withdrawn(sdp))) {
1235                if (fl->fl_type == F_UNLCK)
1236                        locks_lock_file_wait(file, fl);
1237                return -EIO;
1238        }
1239        if (IS_GETLK(cmd))
1240                return dlm_posix_get(ls->ls_dlm, ip->i_no_addr, file, fl);
1241        else if (fl->fl_type == F_UNLCK)
1242                return dlm_posix_unlock(ls->ls_dlm, ip->i_no_addr, file, fl);
1243        else
1244                return dlm_posix_lock(ls->ls_dlm, ip->i_no_addr, file, cmd, fl);
1245}
1246
1247static int do_flock(struct file *file, int cmd, struct file_lock *fl)
1248{
1249        struct gfs2_file *fp = file->private_data;
1250        struct gfs2_holder *fl_gh = &fp->f_fl_gh;
1251        struct gfs2_inode *ip = GFS2_I(file_inode(file));
1252        struct gfs2_glock *gl;
1253        unsigned int state;
1254        u16 flags;
1255        int error = 0;
1256        int sleeptime;
1257
1258        state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
1259        flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY_1CB) | GL_EXACT;
1260
1261        mutex_lock(&fp->f_fl_mutex);
1262
1263        if (gfs2_holder_initialized(fl_gh)) {
1264                if (fl_gh->gh_state == state)
1265                        goto out;
1266                locks_lock_file_wait(file,
1267                                     &(struct file_lock) {
1268                                             .fl_type = F_UNLCK,
1269                                             .fl_flags = FL_FLOCK
1270                                     });
1271                gfs2_glock_dq(fl_gh);
1272                gfs2_holder_reinit(state, flags, fl_gh);
1273        } else {
1274                error = gfs2_glock_get(GFS2_SB(&ip->i_inode), ip->i_no_addr,
1275                                       &gfs2_flock_glops, CREATE, &gl);
1276                if (error)
1277                        goto out;
1278                gfs2_holder_init(gl, state, flags, fl_gh);
1279                gfs2_glock_put(gl);
1280        }
1281        for (sleeptime = 1; sleeptime <= 4; sleeptime <<= 1) {
1282                error = gfs2_glock_nq(fl_gh);
1283                if (error != GLR_TRYFAILED)
1284                        break;
1285                fl_gh->gh_flags = LM_FLAG_TRY | GL_EXACT;
1286                fl_gh->gh_error = 0;
1287                msleep(sleeptime);
1288        }
1289        if (error) {
1290                gfs2_holder_uninit(fl_gh);
1291                if (error == GLR_TRYFAILED)
1292                        error = -EAGAIN;
1293        } else {
1294                error = locks_lock_file_wait(file, fl);
1295                gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
1296        }
1297
1298out:
1299        mutex_unlock(&fp->f_fl_mutex);
1300        return error;
1301}
1302
1303static void do_unflock(struct file *file, struct file_lock *fl)
1304{
1305        struct gfs2_file *fp = file->private_data;
1306        struct gfs2_holder *fl_gh = &fp->f_fl_gh;
1307
1308        mutex_lock(&fp->f_fl_mutex);
1309        locks_lock_file_wait(file, fl);
1310        if (gfs2_holder_initialized(fl_gh)) {
1311                gfs2_glock_dq(fl_gh);
1312                gfs2_holder_uninit(fl_gh);
1313        }
1314        mutex_unlock(&fp->f_fl_mutex);
1315}
1316
1317/**
1318 * gfs2_flock - acquire/release a flock lock on a file
1319 * @file: the file pointer
1320 * @cmd: either modify or retrieve lock state, possibly wait
1321 * @fl: type and range of lock
1322 *
1323 * Returns: errno
1324 */
1325
1326static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
1327{
1328        if (!(fl->fl_flags & FL_FLOCK))
1329                return -ENOLCK;
1330        if (fl->fl_type & LOCK_MAND)
1331                return -EOPNOTSUPP;
1332
1333        if (fl->fl_type == F_UNLCK) {
1334                do_unflock(file, fl);
1335                return 0;
1336        } else {
1337                return do_flock(file, cmd, fl);
1338        }
1339}
1340
1341const struct file_operations gfs2_file_fops = {
1342        .llseek         = gfs2_llseek,
1343        .read_iter      = gfs2_file_read_iter,
1344        .write_iter     = gfs2_file_write_iter,
1345        .iopoll         = iomap_dio_iopoll,
1346        .unlocked_ioctl = gfs2_ioctl,
1347        .mmap           = gfs2_mmap,
1348        .open           = gfs2_open,
1349        .release        = gfs2_release,
1350        .fsync          = gfs2_fsync,
1351        .lock           = gfs2_lock,
1352        .flock          = gfs2_flock,
1353        .splice_read    = generic_file_splice_read,
1354        .splice_write   = gfs2_file_splice_write,
1355        .setlease       = simple_nosetlease,
1356        .fallocate      = gfs2_fallocate,
1357};
1358
1359const struct file_operations gfs2_dir_fops = {
1360        .iterate_shared = gfs2_readdir,
1361        .unlocked_ioctl = gfs2_ioctl,
1362        .open           = gfs2_open,
1363        .release        = gfs2_release,
1364        .fsync          = gfs2_fsync,
1365        .lock           = gfs2_lock,
1366        .flock          = gfs2_flock,
1367        .llseek         = default_llseek,
1368};
1369
1370#endif /* CONFIG_GFS2_FS_LOCKING_DLM */
1371
1372const struct file_operations gfs2_file_fops_nolock = {
1373        .llseek         = gfs2_llseek,
1374        .read_iter      = gfs2_file_read_iter,
1375        .write_iter     = gfs2_file_write_iter,
1376        .iopoll         = iomap_dio_iopoll,
1377        .unlocked_ioctl = gfs2_ioctl,
1378        .mmap           = gfs2_mmap,
1379        .open           = gfs2_open,
1380        .release        = gfs2_release,
1381        .fsync          = gfs2_fsync,
1382        .splice_read    = generic_file_splice_read,
1383        .splice_write   = gfs2_file_splice_write,
1384        .setlease       = generic_setlease,
1385        .fallocate      = gfs2_fallocate,
1386};
1387
1388const struct file_operations gfs2_dir_fops_nolock = {
1389        .iterate_shared = gfs2_readdir,
1390        .unlocked_ioctl = gfs2_ioctl,
1391        .open           = gfs2_open,
1392        .release        = gfs2_release,
1393        .fsync          = gfs2_fsync,
1394        .llseek         = default_llseek,
1395};
1396
1397