linux/fs/gfs2/inode.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
   3 * Copyright (C) 2004-2011 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/namei.h>
  15#include <linux/mm.h>
  16#include <linux/xattr.h>
  17#include <linux/posix_acl.h>
  18#include <linux/gfs2_ondisk.h>
  19#include <linux/crc32.h>
  20#include <linux/fiemap.h>
  21#include <linux/security.h>
  22#include <asm/uaccess.h>
  23
  24#include "gfs2.h"
  25#include "incore.h"
  26#include "acl.h"
  27#include "bmap.h"
  28#include "dir.h"
  29#include "xattr.h"
  30#include "glock.h"
  31#include "inode.h"
  32#include "meta_io.h"
  33#include "quota.h"
  34#include "rgrp.h"
  35#include "trans.h"
  36#include "util.h"
  37#include "super.h"
  38#include "glops.h"
  39
  40static int iget_test(struct inode *inode, void *opaque)
  41{
  42        u64 no_addr = *(u64 *)opaque;
  43
  44        return GFS2_I(inode)->i_no_addr == no_addr;
  45}
  46
  47static int iget_set(struct inode *inode, void *opaque)
  48{
  49        u64 no_addr = *(u64 *)opaque;
  50
  51        GFS2_I(inode)->i_no_addr = no_addr;
  52        inode->i_ino = no_addr;
  53        return 0;
  54}
  55
  56static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
  57{
  58        struct inode *inode;
  59
  60repeat:
  61        inode = iget5_locked(sb, no_addr, iget_test, iget_set, &no_addr);
  62        if (!inode)
  63                return inode;
  64        if (is_bad_inode(inode)) {
  65                iput(inode);
  66                goto repeat;
  67        }
  68        return inode;
  69}
  70
  71/**
  72 * gfs2_set_iop - Sets inode operations
  73 * @inode: The inode with correct i_mode filled in
  74 *
  75 * GFS2 lookup code fills in vfs inode contents based on info obtained
  76 * from directory entry inside gfs2_inode_lookup().
  77 */
  78
  79static void gfs2_set_iop(struct inode *inode)
  80{
  81        struct gfs2_sbd *sdp = GFS2_SB(inode);
  82        umode_t mode = inode->i_mode;
  83
  84        if (S_ISREG(mode)) {
  85                inode->i_op = &gfs2_file_iops;
  86                if (gfs2_localflocks(sdp))
  87                        inode->i_fop = &gfs2_file_fops_nolock;
  88                else
  89                        inode->i_fop = &gfs2_file_fops;
  90        } else if (S_ISDIR(mode)) {
  91                inode->i_flags |= S_IOPS_WRAPPER;
  92                inode->i_op = &gfs2_dir_iops.ops;
  93                if (gfs2_localflocks(sdp))
  94                        inode->i_fop = &gfs2_dir_fops_nolock;
  95                else
  96                        inode->i_fop = &gfs2_dir_fops;
  97        } else if (S_ISLNK(mode)) {
  98                inode->i_op = &gfs2_symlink_iops;
  99        } else {
 100                inode->i_op = &gfs2_file_iops;
 101                init_special_inode(inode, inode->i_mode, inode->i_rdev);
 102        }
 103}
 104
 105/**
 106 * gfs2_inode_lookup - Lookup an inode
 107 * @sb: The super block
 108 * @type: The type of the inode
 109 * @no_addr: The inode number
 110 * @no_formal_ino: The inode generation number
 111 * @blktype: Requested block type (GFS2_BLKST_DINODE or GFS2_BLKST_UNLINKED;
 112 *           GFS2_BLKST_FREE do indicate not to verify)
 113 *
 114 * If @type is DT_UNKNOWN, the inode type is fetched from disk.
 115 *
 116 * If @blktype is anything other than GFS2_BLKST_FREE (which is used as a
 117 * placeholder because it doesn't otherwise make sense), the on-disk block type
 118 * is verified to be @blktype.
 119 *
 120 * Returns: A VFS inode, or an error
 121 */
 122
 123struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
 124                                u64 no_addr, u64 no_formal_ino,
 125                                unsigned int blktype)
 126{
 127        struct inode *inode;
 128        struct gfs2_inode *ip;
 129        struct gfs2_glock *io_gl = NULL;
 130        struct gfs2_holder i_gh;
 131        int error;
 132
 133        gfs2_holder_mark_uninitialized(&i_gh);
 134        inode = gfs2_iget(sb, no_addr);
 135        if (!inode)
 136                return ERR_PTR(-ENOMEM);
 137
 138        ip = GFS2_I(inode);
 139
 140        if (inode->i_state & I_NEW) {
 141                struct gfs2_sbd *sdp = GFS2_SB(inode);
 142                ip->i_no_formal_ino = no_formal_ino;
 143
 144                error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
 145                if (unlikely(error))
 146                        goto fail;
 147                ip->i_gl->gl_object = ip;
 148
 149                error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
 150                if (unlikely(error))
 151                        goto fail_put;
 152
 153                if (type == DT_UNKNOWN || blktype != GFS2_BLKST_FREE) {
 154                        /*
 155                         * The GL_SKIP flag indicates to skip reading the inode
 156                         * block.  We read the inode with gfs2_inode_refresh
 157                         * after possibly checking the block type.
 158                         */
 159                        error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE,
 160                                                   GL_SKIP, &i_gh);
 161                        if (error)
 162                                goto fail_put;
 163
 164                        if (blktype != GFS2_BLKST_FREE) {
 165                                error = gfs2_check_blk_type(sdp, no_addr,
 166                                                            blktype);
 167                                if (error)
 168                                        goto fail_put;
 169                        }
 170                }
 171
 172                set_bit(GIF_INVALID, &ip->i_flags);
 173                error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
 174                if (unlikely(error))
 175                        goto fail_put;
 176
 177                ip->i_iopen_gh.gh_gl->gl_object = ip;
 178                gfs2_glock_put(io_gl);
 179                io_gl = NULL;
 180
 181                if (type == DT_UNKNOWN) {
 182                        /* Inode glock must be locked already */
 183                        error = gfs2_inode_refresh(GFS2_I(inode));
 184                        if (error)
 185                                goto fail_refresh;
 186                } else {
 187                        inode->i_mode = DT2IF(type);
 188                }
 189
 190                gfs2_set_iop(inode);
 191
 192                inode->i_atime.tv_sec = 0;
 193                inode->i_atime.tv_nsec = 0;
 194
 195                unlock_new_inode(inode);
 196        }
 197
 198        if (gfs2_holder_initialized(&i_gh))
 199                gfs2_glock_dq_uninit(&i_gh);
 200        return inode;
 201
 202fail_refresh:
 203        ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
 204        ip->i_iopen_gh.gh_gl->gl_object = NULL;
 205        gfs2_glock_dq_wait(&ip->i_iopen_gh);
 206        gfs2_holder_uninit(&ip->i_iopen_gh);
 207fail_put:
 208        if (io_gl)
 209                gfs2_glock_put(io_gl);
 210        if (gfs2_holder_initialized(&i_gh))
 211                gfs2_glock_dq_uninit(&i_gh);
 212        ip->i_gl->gl_object = NULL;
 213fail:
 214        iget_failed(inode);
 215        return ERR_PTR(error);
 216}
 217
 218struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
 219                                  u64 *no_formal_ino, unsigned int blktype)
 220{
 221        struct super_block *sb = sdp->sd_vfs;
 222        struct inode *inode;
 223        int error;
 224
 225        inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, blktype);
 226        if (IS_ERR(inode))
 227                return inode;
 228
 229        /* Two extra checks for NFS only */
 230        if (no_formal_ino) {
 231                error = -ESTALE;
 232                if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
 233                        goto fail_iput;
 234
 235                error = -EIO;
 236                if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
 237                        goto fail_iput;
 238        }
 239        return inode;
 240
 241fail_iput:
 242        iput(inode);
 243        return ERR_PTR(error);
 244}
 245
 246
 247struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
 248{
 249        struct qstr qstr;
 250        struct inode *inode;
 251        gfs2_str2qstr(&qstr, name);
 252        inode = gfs2_lookupi(dip, &qstr, 1);
 253        /* gfs2_lookupi has inconsistent callers: vfs
 254         * related routines expect NULL for no entry found,
 255         * gfs2_lookup_simple callers expect ENOENT
 256         * and do not check for NULL.
 257         */
 258        if (inode == NULL)
 259                return ERR_PTR(-ENOENT);
 260        else
 261                return inode;
 262}
 263
 264
 265/**
 266 * gfs2_lookupi - Look up a filename in a directory and return its inode
 267 * @d_gh: An initialized holder for the directory glock
 268 * @name: The name of the inode to look for
 269 * @is_root: If 1, ignore the caller's permissions
 270 * @i_gh: An uninitialized holder for the new inode glock
 271 *
 272 * This can be called via the VFS filldir function when NFS is doing
 273 * a readdirplus and the inode which its intending to stat isn't
 274 * already in cache. In this case we must not take the directory glock
 275 * again, since the readdir call will have already taken that lock.
 276 *
 277 * Returns: errno
 278 */
 279
 280struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
 281                           int is_root)
 282{
 283        struct super_block *sb = dir->i_sb;
 284        struct gfs2_inode *dip = GFS2_I(dir);
 285        struct gfs2_holder d_gh;
 286        int error = 0;
 287        struct inode *inode = NULL;
 288
 289        gfs2_holder_mark_uninitialized(&d_gh);
 290        if (!name->len || name->len > GFS2_FNAMESIZE)
 291                return ERR_PTR(-ENAMETOOLONG);
 292
 293        if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
 294            (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
 295             dir == sb->s_root->d_inode)) {
 296                igrab(dir);
 297                return dir;
 298        }
 299
 300        if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
 301                error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
 302                if (error)
 303                        return ERR_PTR(error);
 304        }
 305
 306        if (!is_root) {
 307                error = gfs2_permission(dir, MAY_EXEC);
 308                if (error)
 309                        goto out;
 310        }
 311
 312        inode = gfs2_dir_search(dir, name, false);
 313        if (IS_ERR(inode))
 314                error = PTR_ERR(inode);
 315out:
 316        if (gfs2_holder_initialized(&d_gh))
 317                gfs2_glock_dq_uninit(&d_gh);
 318        if (error == -ENOENT)
 319                return NULL;
 320        return inode ? inode : ERR_PTR(error);
 321}
 322
 323/**
 324 * create_ok - OK to create a new on-disk inode here?
 325 * @dip:  Directory in which dinode is to be created
 326 * @name:  Name of new dinode
 327 * @mode:
 328 *
 329 * Returns: errno
 330 */
 331
 332static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
 333                     umode_t mode)
 334{
 335        int error;
 336
 337        error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
 338        if (error)
 339                return error;
 340
 341        /*  Don't create entries in an unlinked directory  */
 342        if (!dip->i_inode.i_nlink)
 343                return -ENOENT;
 344
 345        if (dip->i_entries == (u32)-1)
 346                return -EFBIG;
 347        if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
 348                return -EMLINK;
 349
 350        return 0;
 351}
 352
 353static void munge_mode_uid_gid(const struct gfs2_inode *dip,
 354                               struct inode *inode)
 355{
 356        if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
 357            (dip->i_inode.i_mode & S_ISUID) &&
 358            !uid_eq(dip->i_inode.i_uid, GLOBAL_ROOT_UID)) {
 359                if (S_ISDIR(inode->i_mode))
 360                        inode->i_mode |= S_ISUID;
 361                else if (!uid_eq(dip->i_inode.i_uid, current_fsuid()))
 362                        inode->i_mode &= ~07111;
 363                inode->i_uid = dip->i_inode.i_uid;
 364        } else
 365                inode->i_uid = current_fsuid();
 366
 367        if (dip->i_inode.i_mode & S_ISGID) {
 368                if (S_ISDIR(inode->i_mode))
 369                        inode->i_mode |= S_ISGID;
 370                inode->i_gid = dip->i_inode.i_gid;
 371        } else
 372                inode->i_gid = current_fsgid();
 373}
 374
 375static int alloc_dinode(struct gfs2_inode *ip, u32 flags)
 376{
 377        struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
 378        struct gfs2_alloc_parms ap = { .target = RES_DINODE, .aflags = flags, };
 379        int error;
 380        int dblocks = 1;
 381
 382        error = gfs2_quota_lock_check(ip, &ap);
 383        if (error)
 384                goto out;
 385
 386        error = gfs2_inplace_reserve(ip, &ap);
 387        if (error)
 388                goto out_quota;
 389
 390        error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 0);
 391        if (error)
 392                goto out_ipreserv;
 393
 394        error = gfs2_alloc_blocks(ip, &ip->i_no_addr, &dblocks, 1, &ip->i_generation);
 395        ip->i_no_formal_ino = ip->i_generation;
 396        ip->i_inode.i_ino = ip->i_no_addr;
 397        ip->i_goal = ip->i_no_addr;
 398
 399        gfs2_trans_end(sdp);
 400
 401out_ipreserv:
 402        gfs2_inplace_release(ip);
 403out_quota:
 404        gfs2_quota_unlock(ip);
 405out:
 406        return error;
 407}
 408
 409static void gfs2_init_dir(struct buffer_head *dibh,
 410                          const struct gfs2_inode *parent)
 411{
 412        struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
 413        struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
 414
 415        gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
 416        dent->de_inum = di->di_num; /* already GFS2 endian */
 417        dent->de_type = cpu_to_be16(DT_DIR);
 418
 419        dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
 420        gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
 421        gfs2_inum_out(parent, dent);
 422        dent->de_type = cpu_to_be16(DT_DIR);
 423        
 424}
 425
 426/**
 427 * init_dinode - Fill in a new dinode structure
 428 * @dip: The directory this inode is being created in
 429 * @ip: The inode
 430 * @symname: The symlink destination (if a symlink)
 431 * @bhp: The buffer head (returned to caller)
 432 *
 433 */
 434
 435static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
 436                        const char *symname)
 437{
 438        struct gfs2_dinode *di;
 439        struct buffer_head *dibh;
 440
 441        dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr);
 442        gfs2_trans_add_meta(ip->i_gl, dibh);
 443        di = (struct gfs2_dinode *)dibh->b_data;
 444        gfs2_dinode_out(ip, di);
 445
 446        di->di_major = cpu_to_be32(MAJOR(ip->i_inode.i_rdev));
 447        di->di_minor = cpu_to_be32(MINOR(ip->i_inode.i_rdev));
 448        di->__pad1 = 0;
 449        di->__pad2 = 0;
 450        di->__pad3 = 0;
 451        memset(&di->__pad4, 0, sizeof(di->__pad4));
 452        memset(&di->di_reserved, 0, sizeof(di->di_reserved));
 453        gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
 454
 455        switch(ip->i_inode.i_mode & S_IFMT) {
 456        case S_IFDIR:
 457                gfs2_init_dir(dibh, dip);
 458                break;
 459        case S_IFLNK:
 460                memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size);
 461                break;
 462        }
 463
 464        set_buffer_uptodate(dibh);
 465        brelse(dibh);
 466}
 467
 468static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
 469                       struct gfs2_inode *ip, int arq)
 470{
 471        struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
 472        struct gfs2_alloc_parms ap = { .target = sdp->sd_max_dirres, };
 473        int error;
 474
 475        if (arq) {
 476                error = gfs2_quota_lock_check(dip, &ap);
 477                if (error)
 478                        goto fail_quota_locks;
 479
 480                error = gfs2_inplace_reserve(dip, &ap);
 481                if (error)
 482                        goto fail_quota_locks;
 483
 484                error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
 485                                         dip->i_rgd->rd_length +
 486                                         2 * RES_DINODE +
 487                                         RES_STATFS + RES_QUOTA, 0);
 488                if (error)
 489                        goto fail_ipreserv;
 490        } else {
 491                error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
 492                if (error)
 493                        goto fail_quota_locks;
 494        }
 495
 496        error = gfs2_dir_add(&dip->i_inode, name, ip);
 497        if (error)
 498                goto fail_end_trans;
 499
 500fail_end_trans:
 501        gfs2_trans_end(sdp);
 502fail_ipreserv:
 503        gfs2_inplace_release(dip);
 504fail_quota_locks:
 505        gfs2_quota_unlock(dip);
 506        return error;
 507}
 508
 509static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
 510                    void *fs_info)
 511{
 512        const struct xattr *xattr;
 513        int err = 0;
 514
 515        for (xattr = xattr_array; xattr->name != NULL; xattr++) {
 516                err = __gfs2_xattr_set(inode, xattr->name, xattr->value,
 517                                       xattr->value_len, 0,
 518                                       GFS2_EATYPE_SECURITY);
 519                if (err < 0)
 520                        break;
 521        }
 522        return err;
 523}
 524
 525static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip,
 526                              const struct qstr *qstr)
 527{
 528        return security_inode_init_security(&ip->i_inode, &dip->i_inode, qstr,
 529                                            &gfs2_initxattrs, NULL);
 530}
 531
 532/**
 533 * gfs2_create_inode - Create a new inode
 534 * @dir: The parent directory
 535 * @dentry: The new dentry
 536 * @file: If non-NULL, the file which is being opened
 537 * @mode: The permissions on the new inode
 538 * @dev: For device nodes, this is the device number
 539 * @symname: For symlinks, this is the link destination
 540 * @size: The initial size of the inode (ignored for directories)
 541 *
 542 * Returns: 0 on success, or error code
 543 */
 544
 545static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
 546                             struct file *file,
 547                             umode_t mode, dev_t dev, const char *symname,
 548                             unsigned int size, int excl, int *opened)
 549{
 550        const struct qstr *name = &dentry->d_name;
 551        struct gfs2_holder ghs[2];
 552        struct inode *inode = NULL;
 553        struct gfs2_inode *dip = GFS2_I(dir), *ip;
 554        struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
 555        struct gfs2_glock *io_gl = NULL;
 556        struct dentry *d;
 557        int error, free_vfs_inode = 1;
 558        u32 aflags = 0;
 559        int arq;
 560
 561        if (!name->len || name->len > GFS2_FNAMESIZE)
 562                return -ENAMETOOLONG;
 563
 564        error = gfs2_rsqa_alloc(dip);
 565        if (error)
 566                return error;
 567
 568        error = gfs2_rindex_update(sdp);
 569        if (error)
 570                return error;
 571
 572        error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
 573        if (error)
 574                goto fail;
 575
 576        error = create_ok(dip, name, mode);
 577        if (error)
 578                goto fail_gunlock;
 579
 580        inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
 581        error = PTR_ERR(inode);
 582        if (!IS_ERR(inode)) {
 583                d = d_splice_alias(inode, dentry);
 584                error = PTR_ERR(d);
 585                if (IS_ERR(d)) {
 586                        inode = ERR_CAST(d);
 587                        goto fail_gunlock;
 588                }
 589                error = 0;
 590                if (file) {
 591                        if (S_ISREG(inode->i_mode)) {
 592                                WARN_ON(d != NULL);
 593                                error = finish_open(file, dentry, gfs2_open_common, opened);
 594                        } else {
 595                                error = finish_no_open(file, d);
 596                        }
 597                } else {
 598                        dput(d);
 599                }
 600                gfs2_glock_dq_uninit(ghs);
 601                return error;
 602        } else if (error != -ENOENT) {
 603                goto fail_gunlock;
 604        }
 605
 606        arq = error = gfs2_diradd_alloc_required(dir, name);
 607        if (error < 0)
 608                goto fail_gunlock;
 609
 610        inode = new_inode(sdp->sd_vfs);
 611        error = -ENOMEM;
 612        if (!inode)
 613                goto fail_gunlock;
 614
 615        ip = GFS2_I(inode);
 616        error = gfs2_rsqa_alloc(ip);
 617        if (error)
 618                goto fail_free_inode;
 619
 620        inode->i_mode = mode;
 621        set_nlink(inode, S_ISDIR(mode) ? 2 : 1);
 622        inode->i_rdev = dev;
 623        inode->i_size = size;
 624        inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
 625        gfs2_set_inode_blocks(inode, 1);
 626        munge_mode_uid_gid(dip, inode);
 627        check_and_update_goal(dip);
 628        ip->i_goal = dip->i_goal;
 629        ip->i_diskflags = 0;
 630        ip->i_eattr = 0;
 631        ip->i_height = 0;
 632        ip->i_depth = 0;
 633        ip->i_entries = 0;
 634
 635        switch(mode & S_IFMT) {
 636        case S_IFREG:
 637                if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
 638                    gfs2_tune_get(sdp, gt_new_files_jdata))
 639                        ip->i_diskflags |= GFS2_DIF_JDATA;
 640                gfs2_set_aops(inode);
 641                break;
 642        case S_IFDIR:
 643                ip->i_diskflags |= (dip->i_diskflags & GFS2_DIF_INHERIT_JDATA);
 644                ip->i_diskflags |= GFS2_DIF_JDATA;
 645                ip->i_entries = 2;
 646                break;
 647        }
 648
 649        /* Force SYSTEM flag on all files and subdirs of a SYSTEM directory */
 650        if (dip->i_diskflags & GFS2_DIF_SYSTEM)
 651                ip->i_diskflags |= GFS2_DIF_SYSTEM;
 652
 653        gfs2_set_inode_flags(inode);
 654
 655        if ((GFS2_I(sdp->sd_root_dir->d_inode) == dip) ||
 656            (dip->i_diskflags & GFS2_DIF_TOPDIR))
 657                aflags |= GFS2_AF_ORLOV;
 658
 659        error = alloc_dinode(ip, aflags);
 660        if (error)
 661                goto fail_free_inode;
 662
 663        error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
 664        if (error)
 665                goto fail_free_inode;
 666
 667        ip->i_gl->gl_object = ip;
 668        error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
 669        if (error)
 670                goto fail_free_inode;
 671
 672        error = gfs2_trans_begin(sdp, RES_DINODE, 0);
 673        if (error)
 674                goto fail_gunlock2;
 675
 676        init_dinode(dip, ip, symname);
 677        gfs2_trans_end(sdp);
 678
 679        error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
 680        if (error)
 681                goto fail_gunlock2;
 682
 683        BUG_ON(test_and_set_bit(GLF_INODE_CREATING, &io_gl->gl_flags));
 684
 685        error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
 686        if (error)
 687                goto fail_gunlock2;
 688
 689        ip->i_iopen_gh.gh_gl->gl_object = ip;
 690        gfs2_glock_put(io_gl);
 691        gfs2_set_iop(inode);
 692        insert_inode_hash(inode);
 693
 694        free_vfs_inode = 0; /* After this point, the inode is no longer
 695                               considered free. Any failures need to undo
 696                               the gfs2 structures. */
 697
 698        error = gfs2_acl_create(dip, inode);
 699        if (error)
 700                goto fail_gunlock3;
 701
 702        error = gfs2_security_init(dip, ip, name);
 703        if (error)
 704                goto fail_gunlock3;
 705
 706        error = link_dinode(dip, name, ip, arq);
 707        if (error)
 708                goto fail_gunlock3;
 709
 710        mark_inode_dirty(inode);
 711        d_instantiate(dentry, inode);
 712        if (file) {
 713                *opened |= FILE_CREATED;
 714                error = finish_open(file, dentry, gfs2_open_common, opened);
 715        }
 716        gfs2_glock_dq_uninit(ghs);
 717        gfs2_glock_dq_uninit(ghs + 1);
 718        clear_bit(GLF_INODE_CREATING, &io_gl->gl_flags);
 719        return error;
 720
 721fail_gunlock3:
 722        gfs2_glock_dq_uninit(&ip->i_iopen_gh);
 723        gfs2_glock_put(io_gl);
 724fail_gunlock2:
 725        if (io_gl)
 726                clear_bit(GLF_INODE_CREATING, &io_gl->gl_flags);
 727        gfs2_glock_dq_uninit(ghs + 1);
 728fail_free_inode:
 729        if (ip->i_gl)
 730                gfs2_glock_put(ip->i_gl);
 731        gfs2_rsqa_delete(ip, NULL);
 732fail_gunlock:
 733        gfs2_glock_dq_uninit(ghs);
 734        if (inode && !IS_ERR(inode)) {
 735                clear_nlink(inode);
 736                if (!free_vfs_inode)
 737                        mark_inode_dirty(inode);
 738                set_bit(free_vfs_inode ? GIF_FREE_VFS_INODE : GIF_ALLOC_FAILED,
 739                        &GFS2_I(inode)->i_flags);
 740                iput(inode);
 741        }
 742fail:
 743        return error;
 744}
 745
 746/**
 747 * gfs2_create - Create a file
 748 * @dir: The directory in which to create the file
 749 * @dentry: The dentry of the new file
 750 * @mode: The mode of the new file
 751 *
 752 * Returns: errno
 753 */
 754
 755static int gfs2_create(struct inode *dir, struct dentry *dentry,
 756                       umode_t mode, bool excl)
 757{
 758        return gfs2_create_inode(dir, dentry, NULL, S_IFREG | mode, 0, NULL, 0, excl, NULL);
 759}
 760
 761/**
 762 * __gfs2_lookup - Look up a filename in a directory and return its inode
 763 * @dir: The directory inode
 764 * @dentry: The dentry of the new inode
 765 * @file: File to be opened
 766 * @opened: atomic_open flags
 767 *
 768 *
 769 * Returns: errno
 770 */
 771
 772static struct dentry *__gfs2_lookup(struct inode *dir, struct dentry *dentry,
 773                                    struct file *file, int *opened)
 774{
 775        struct inode *inode;
 776        struct dentry *d;
 777        struct gfs2_holder gh;
 778        struct gfs2_glock *gl;
 779        int error;
 780
 781        inode = gfs2_lookupi(dir, &dentry->d_name, 0);
 782        if (inode == NULL) {
 783                d_add(dentry, NULL);
 784                return NULL;
 785        }
 786        if (IS_ERR(inode))
 787                return ERR_CAST(inode);
 788
 789        gl = GFS2_I(inode)->i_gl;
 790        error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
 791        if (error) {
 792                iput(inode);
 793                return ERR_PTR(error);
 794        }
 795
 796        d = d_splice_alias(inode, dentry);
 797        if (IS_ERR(d)) {
 798                gfs2_glock_dq_uninit(&gh);
 799                return d;
 800        }
 801        if (file && S_ISREG(inode->i_mode))
 802                error = finish_open(file, dentry, gfs2_open_common, opened);
 803
 804        gfs2_glock_dq_uninit(&gh);
 805        if (error) {
 806                dput(d);
 807                return ERR_PTR(error);
 808        }
 809        return d;
 810}
 811
 812static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
 813                                  unsigned flags)
 814{
 815        return __gfs2_lookup(dir, dentry, NULL, NULL);
 816}
 817
 818/**
 819 * gfs2_link - Link to a file
 820 * @old_dentry: The inode to link
 821 * @dir: Add link to this directory
 822 * @dentry: The name of the link
 823 *
 824 * Link the inode in "old_dentry" into the directory "dir" with the
 825 * name in "dentry".
 826 *
 827 * Returns: errno
 828 */
 829
 830static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
 831                     struct dentry *dentry)
 832{
 833        struct gfs2_inode *dip = GFS2_I(dir);
 834        struct gfs2_sbd *sdp = GFS2_SB(dir);
 835        struct inode *inode = old_dentry->d_inode;
 836        struct gfs2_inode *ip = GFS2_I(inode);
 837        struct gfs2_holder ghs[2];
 838        struct buffer_head *dibh;
 839        int alloc_required;
 840        int error;
 841
 842        if (S_ISDIR(inode->i_mode))
 843                return -EPERM;
 844
 845        error = gfs2_rsqa_alloc(dip);
 846        if (error)
 847                return error;
 848
 849        gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
 850        gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
 851
 852        error = gfs2_glock_nq(ghs); /* parent */
 853        if (error)
 854                goto out_parent;
 855
 856        error = gfs2_glock_nq(ghs + 1); /* child */
 857        if (error)
 858                goto out_child;
 859
 860        error = -ENOENT;
 861        if (inode->i_nlink == 0)
 862                goto out_gunlock;
 863
 864        error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
 865        if (error)
 866                goto out_gunlock;
 867
 868        error = gfs2_dir_check(dir, &dentry->d_name, NULL);
 869        switch (error) {
 870        case -ENOENT:
 871                break;
 872        case 0:
 873                error = -EEXIST;
 874        default:
 875                goto out_gunlock;
 876        }
 877
 878        error = -EINVAL;
 879        if (!dip->i_inode.i_nlink)
 880                goto out_gunlock;
 881        error = -EFBIG;
 882        if (dip->i_entries == (u32)-1)
 883                goto out_gunlock;
 884        error = -EPERM;
 885        if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
 886                goto out_gunlock;
 887        error = -EINVAL;
 888        if (!ip->i_inode.i_nlink)
 889                goto out_gunlock;
 890        error = -EMLINK;
 891        if (ip->i_inode.i_nlink == (u32)-1)
 892                goto out_gunlock;
 893
 894        alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
 895        if (error < 0)
 896                goto out_gunlock;
 897        error = 0;
 898
 899        if (alloc_required) {
 900                struct gfs2_alloc_parms ap = { .target = sdp->sd_max_dirres, };
 901                error = gfs2_quota_lock_check(dip, &ap);
 902                if (error)
 903                        goto out_gunlock;
 904
 905                error = gfs2_inplace_reserve(dip, &ap);
 906                if (error)
 907                        goto out_gunlock_q;
 908
 909                error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
 910                                         gfs2_rg_blocks(dip, sdp->sd_max_dirres) +
 911                                         2 * RES_DINODE + RES_STATFS +
 912                                         RES_QUOTA, 0);
 913                if (error)
 914                        goto out_ipres;
 915        } else {
 916                error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
 917                if (error)
 918                        goto out_ipres;
 919        }
 920
 921        error = gfs2_meta_inode_buffer(ip, &dibh);
 922        if (error)
 923                goto out_end_trans;
 924
 925        error = gfs2_dir_add(dir, &dentry->d_name, ip);
 926        if (error)
 927                goto out_brelse;
 928
 929        gfs2_trans_add_meta(ip->i_gl, dibh);
 930        inc_nlink(&ip->i_inode);
 931        ip->i_inode.i_ctime = CURRENT_TIME;
 932        ihold(inode);
 933        d_instantiate(dentry, inode);
 934        mark_inode_dirty(inode);
 935
 936out_brelse:
 937        brelse(dibh);
 938out_end_trans:
 939        gfs2_trans_end(sdp);
 940out_ipres:
 941        if (alloc_required)
 942                gfs2_inplace_release(dip);
 943out_gunlock_q:
 944        if (alloc_required)
 945                gfs2_quota_unlock(dip);
 946out_gunlock:
 947        gfs2_glock_dq(ghs + 1);
 948out_child:
 949        gfs2_glock_dq(ghs);
 950out_parent:
 951        gfs2_holder_uninit(ghs);
 952        gfs2_holder_uninit(ghs + 1);
 953        return error;
 954}
 955
 956/*
 957 * gfs2_unlink_ok - check to see that a inode is still in a directory
 958 * @dip: the directory
 959 * @name: the name of the file
 960 * @ip: the inode
 961 *
 962 * Assumes that the lock on (at least) @dip is held.
 963 *
 964 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
 965 */
 966
 967static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
 968                          const struct gfs2_inode *ip)
 969{
 970        int error;
 971
 972        if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
 973                return -EPERM;
 974
 975        if ((dip->i_inode.i_mode & S_ISVTX) &&
 976            !uid_eq(dip->i_inode.i_uid, current_fsuid()) &&
 977            !uid_eq(ip->i_inode.i_uid, current_fsuid()) && !capable(CAP_FOWNER))
 978                return -EPERM;
 979
 980        if (IS_APPEND(&dip->i_inode))
 981                return -EPERM;
 982
 983        error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
 984        if (error)
 985                return error;
 986
 987        error = gfs2_dir_check(&dip->i_inode, name, ip);
 988        if (error)
 989                return error;
 990
 991        return 0;
 992}
 993
 994/**
 995 * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
 996 * @dip: The parent directory
 997 * @name: The name of the entry in the parent directory
 998 * @inode: The inode to be removed
 999 *
1000 * Called with all the locks and in a transaction. This will only be
1001 * called for a directory after it has been checked to ensure it is empty.
1002 *
1003 * Returns: 0 on success, or an error
1004 */
1005
1006static int gfs2_unlink_inode(struct gfs2_inode *dip,
1007                             const struct dentry *dentry)
1008{
1009        struct inode *inode = dentry->d_inode;
1010        struct gfs2_inode *ip = GFS2_I(inode);
1011        int error;
1012
1013        error = gfs2_dir_del(dip, dentry);
1014        if (error)
1015                return error;
1016
1017        ip->i_entries = 0;
1018        inode->i_ctime = CURRENT_TIME;
1019        if (S_ISDIR(inode->i_mode))
1020                clear_nlink(inode);
1021        else
1022                drop_nlink(inode);
1023        mark_inode_dirty(inode);
1024        if (inode->i_nlink == 0)
1025                gfs2_unlink_di(inode);
1026        return 0;
1027}
1028
1029
1030/**
1031 * gfs2_unlink - Unlink an inode (this does rmdir as well)
1032 * @dir: The inode of the directory containing the inode to unlink
1033 * @dentry: The file itself
1034 *
1035 * This routine uses the type of the inode as a flag to figure out
1036 * whether this is an unlink or an rmdir.
1037 *
1038 * Returns: errno
1039 */
1040
1041static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
1042{
1043        struct gfs2_inode *dip = GFS2_I(dir);
1044        struct gfs2_sbd *sdp = GFS2_SB(dir);
1045        struct inode *inode = dentry->d_inode;
1046        struct gfs2_inode *ip = GFS2_I(inode);
1047        struct gfs2_holder ghs[3];
1048        struct gfs2_rgrpd *rgd;
1049        int error;
1050
1051        error = gfs2_rindex_update(sdp);
1052        if (error)
1053                return error;
1054
1055        error = -EROFS;
1056
1057        gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
1058        gfs2_holder_init(ip->i_gl,  LM_ST_EXCLUSIVE, 0, ghs + 1);
1059
1060        rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
1061        if (!rgd)
1062                goto out_inodes;
1063
1064        gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
1065
1066
1067        error = gfs2_glock_nq(ghs); /* parent */
1068        if (error)
1069                goto out_parent;
1070
1071        error = gfs2_glock_nq(ghs + 1); /* child */
1072        if (error)
1073                goto out_child;
1074
1075        error = -ENOENT;
1076        if (inode->i_nlink == 0)
1077                goto out_rgrp;
1078
1079        if (S_ISDIR(inode->i_mode)) {
1080                error = -ENOTEMPTY;
1081                if (ip->i_entries > 2 || inode->i_nlink > 2)
1082                        goto out_rgrp;
1083        }
1084
1085        error = gfs2_glock_nq(ghs + 2); /* rgrp */
1086        if (error)
1087                goto out_rgrp;
1088
1089        error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
1090        if (error)
1091                goto out_gunlock;
1092
1093        error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
1094        if (error)
1095                goto out_end_trans;
1096
1097        error = gfs2_unlink_inode(dip, dentry);
1098
1099out_end_trans:
1100        gfs2_trans_end(sdp);
1101out_gunlock:
1102        gfs2_glock_dq(ghs + 2);
1103out_rgrp:
1104        gfs2_glock_dq(ghs + 1);
1105out_child:
1106        gfs2_glock_dq(ghs);
1107out_parent:
1108        gfs2_holder_uninit(ghs + 2);
1109out_inodes:
1110        gfs2_holder_uninit(ghs + 1);
1111        gfs2_holder_uninit(ghs);
1112        return error;
1113}
1114
1115/**
1116 * gfs2_symlink - Create a symlink
1117 * @dir: The directory to create the symlink in
1118 * @dentry: The dentry to put the symlink in
1119 * @symname: The thing which the link points to
1120 *
1121 * Returns: errno
1122 */
1123
1124static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
1125                        const char *symname)
1126{
1127        struct gfs2_sbd *sdp = GFS2_SB(dir);
1128        unsigned int size;
1129
1130        size = strlen(symname);
1131        if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
1132                return -ENAMETOOLONG;
1133
1134        return gfs2_create_inode(dir, dentry, NULL, S_IFLNK | S_IRWXUGO, 0, symname, size, 0, NULL);
1135}
1136
1137/**
1138 * gfs2_mkdir - Make a directory
1139 * @dir: The parent directory of the new one
1140 * @dentry: The dentry of the new directory
1141 * @mode: The mode of the new directory
1142 *
1143 * Returns: errno
1144 */
1145
1146static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1147{
1148        struct gfs2_sbd *sdp = GFS2_SB(dir);
1149        unsigned dsize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
1150        return gfs2_create_inode(dir, dentry, NULL, S_IFDIR | mode, 0, NULL, dsize, 0, NULL);
1151}
1152
1153/**
1154 * gfs2_mknod - Make a special file
1155 * @dir: The directory in which the special file will reside
1156 * @dentry: The dentry of the special file
1157 * @mode: The mode of the special file
1158 * @dev: The device specification of the special file
1159 *
1160 */
1161
1162static int gfs2_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
1163                      dev_t dev)
1164{
1165        return gfs2_create_inode(dir, dentry, NULL, mode, dev, NULL, 0, 0, NULL);
1166}
1167
1168/**
1169 * gfs2_atomic_open - Atomically open a file
1170 * @dir: The directory
1171 * @dentry: The proposed new entry
1172 * @file: The proposed new struct file
1173 * @flags: open flags
1174 * @mode: File mode
1175 * @opened: Flag to say whether the file has been opened or not
1176 *
1177 * Returns: error code or 0 for success
1178 */
1179
1180static int gfs2_atomic_open(struct inode *dir, struct dentry *dentry,
1181                            struct file *file, unsigned flags,
1182                            umode_t mode, int *opened)
1183{
1184        struct dentry *d;
1185        bool excl = !!(flags & O_EXCL);
1186
1187        if (!d_unhashed(dentry))
1188                goto skip_lookup;
1189
1190        d = __gfs2_lookup(dir, dentry, file, opened);
1191        if (IS_ERR(d))
1192                return PTR_ERR(d);
1193        if (d != NULL)
1194                dentry = d;
1195        if (dentry->d_inode) {
1196                if (!(*opened & FILE_OPENED)) {
1197                        if (d == NULL)
1198                                dget(dentry);
1199                        return finish_no_open(file, dentry);
1200                }
1201                dput(d);
1202                return 0;
1203        }
1204
1205        BUG_ON(d != NULL);
1206
1207skip_lookup:
1208        if (!(flags & O_CREAT))
1209                return -ENOENT;
1210
1211        return gfs2_create_inode(dir, dentry, file, S_IFREG | mode, 0, NULL, 0, excl, opened);
1212}
1213
1214/*
1215 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1216 * @this: move this
1217 * @to: to here
1218 *
1219 * Follow @to back to the root and make sure we don't encounter @this
1220 * Assumes we already hold the rename lock.
1221 *
1222 * Returns: errno
1223 */
1224
1225static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1226{
1227        struct inode *dir = &to->i_inode;
1228        struct super_block *sb = dir->i_sb;
1229        struct inode *tmp;
1230        int error = 0;
1231
1232        igrab(dir);
1233
1234        for (;;) {
1235                if (dir == &this->i_inode) {
1236                        error = -EINVAL;
1237                        break;
1238                }
1239                if (dir == sb->s_root->d_inode) {
1240                        error = 0;
1241                        break;
1242                }
1243
1244                tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
1245                if (!tmp) {
1246                        error = -ENOENT;
1247                        break;
1248                }
1249                if (IS_ERR(tmp)) {
1250                        error = PTR_ERR(tmp);
1251                        break;
1252                }
1253
1254                iput(dir);
1255                dir = tmp;
1256        }
1257
1258        iput(dir);
1259
1260        return error;
1261}
1262
1263/**
1264 * update_moved_ino - Update an inode that's being moved
1265 * @ip: The inode being moved
1266 * @ndip: The parent directory of the new filename
1267 * @dir_rename: True of ip is a directory
1268 *
1269 * Returns: errno
1270 */
1271
1272static int update_moved_ino(struct gfs2_inode *ip, struct gfs2_inode *ndip,
1273                            int dir_rename)
1274{
1275        int error;
1276        struct buffer_head *dibh;
1277
1278        if (dir_rename)
1279                return gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
1280
1281        error = gfs2_meta_inode_buffer(ip, &dibh);
1282        if (error)
1283                return error;
1284        ip->i_inode.i_ctime = CURRENT_TIME;
1285        gfs2_trans_add_meta(ip->i_gl, dibh);
1286        gfs2_dinode_out(ip, dibh->b_data);
1287        brelse(dibh);
1288        return 0;
1289}
1290
1291
1292/**
1293 * gfs2_rename - Rename a file
1294 * @odir: Parent directory of old file name
1295 * @odentry: The old dentry of the file
1296 * @ndir: Parent directory of new file name
1297 * @ndentry: The new dentry of the file
1298 *
1299 * Returns: errno
1300 */
1301
1302static int gfs2_rename(struct inode *odir, struct dentry *odentry,
1303                       struct inode *ndir, struct dentry *ndentry)
1304{
1305        struct gfs2_inode *odip = GFS2_I(odir);
1306        struct gfs2_inode *ndip = GFS2_I(ndir);
1307        struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
1308        struct gfs2_inode *nip = NULL;
1309        struct gfs2_sbd *sdp = GFS2_SB(odir);
1310        struct gfs2_holder ghs[5], r_gh;
1311        struct gfs2_rgrpd *nrgd;
1312        unsigned int num_gh;
1313        int dir_rename = 0;
1314        int alloc_required = 0;
1315        unsigned int x;
1316        int error;
1317
1318        gfs2_holder_mark_uninitialized(&r_gh);
1319        if (ndentry->d_inode) {
1320                nip = GFS2_I(ndentry->d_inode);
1321                if (ip == nip)
1322                        return 0;
1323        }
1324
1325        error = gfs2_rindex_update(sdp);
1326        if (error)
1327                return error;
1328
1329        error = gfs2_rsqa_alloc(ndip);
1330        if (error)
1331                return error;
1332
1333        if (odip != ndip) {
1334                error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1335                                           0, &r_gh);
1336                if (error)
1337                        goto out;
1338
1339                if (S_ISDIR(ip->i_inode.i_mode)) {
1340                        dir_rename = 1;
1341                        /* don't move a directory into its subdir */
1342                        error = gfs2_ok_to_move(ip, ndip);
1343                        if (error)
1344                                goto out_gunlock_r;
1345                }
1346        }
1347
1348        num_gh = 1;
1349        gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
1350        if (odip != ndip) {
1351                gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
1352                num_gh++;
1353        }
1354        gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
1355        num_gh++;
1356
1357        if (nip) {
1358                gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
1359                num_gh++;
1360                /* grab the resource lock for unlink flag twiddling 
1361                 * this is the case of the target file already existing
1362                 * so we unlink before doing the rename
1363                 */
1364                nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
1365                if (nrgd)
1366                        gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
1367        }
1368
1369        for (x = 0; x < num_gh; x++) {
1370                error = gfs2_glock_nq(ghs + x);
1371                if (error)
1372                        goto out_gunlock;
1373        }
1374
1375        error = -ENOENT;
1376        if (ip->i_inode.i_nlink == 0)
1377                goto out_gunlock;
1378
1379        /* Check out the old directory */
1380
1381        error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
1382        if (error)
1383                goto out_gunlock;
1384
1385        /* Check out the new directory */
1386
1387        if (nip) {
1388                error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
1389                if (error)
1390                        goto out_gunlock;
1391
1392                if (nip->i_inode.i_nlink == 0) {
1393                        error = -EAGAIN;
1394                        goto out_gunlock;
1395                }
1396
1397                if (S_ISDIR(nip->i_inode.i_mode)) {
1398                        if (nip->i_entries < 2) {
1399                                gfs2_consist_inode(nip);
1400                                error = -EIO;
1401                                goto out_gunlock;
1402                        }
1403                        if (nip->i_entries > 2) {
1404                                error = -ENOTEMPTY;
1405                                goto out_gunlock;
1406                        }
1407                }
1408        } else {
1409                error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
1410                if (error)
1411                        goto out_gunlock;
1412
1413                error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
1414                switch (error) {
1415                case -ENOENT:
1416                        error = 0;
1417                        break;
1418                case 0:
1419                        error = -EEXIST;
1420                default:
1421                        goto out_gunlock;
1422                };
1423
1424                if (odip != ndip) {
1425                        if (!ndip->i_inode.i_nlink) {
1426                                error = -ENOENT;
1427                                goto out_gunlock;
1428                        }
1429                        if (ndip->i_entries == (u32)-1) {
1430                                error = -EFBIG;
1431                                goto out_gunlock;
1432                        }
1433                        if (S_ISDIR(ip->i_inode.i_mode) &&
1434                            ndip->i_inode.i_nlink == (u32)-1) {
1435                                error = -EMLINK;
1436                                goto out_gunlock;
1437                        }
1438                }
1439        }
1440
1441        /* Check out the dir to be renamed */
1442
1443        if (dir_rename) {
1444                error = gfs2_permission(odentry->d_inode, MAY_WRITE);
1445                if (error)
1446                        goto out_gunlock;
1447        }
1448
1449        if (nip == NULL)
1450                alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
1451        error = alloc_required;
1452        if (error < 0)
1453                goto out_gunlock;
1454
1455        if (alloc_required) {
1456                struct gfs2_alloc_parms ap = { .target = sdp->sd_max_dirres, };
1457                error = gfs2_quota_lock_check(ndip, &ap);
1458                if (error)
1459                        goto out_gunlock;
1460
1461                error = gfs2_inplace_reserve(ndip, &ap);
1462                if (error)
1463                        goto out_gunlock_q;
1464
1465                error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
1466                                         gfs2_rg_blocks(ndip, sdp->sd_max_dirres) +
1467                                         4 * RES_DINODE + 4 * RES_LEAF +
1468                                         RES_STATFS + RES_QUOTA + 4, 0);
1469                if (error)
1470                        goto out_ipreserv;
1471        } else {
1472                error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
1473                                         5 * RES_LEAF + 4, 0);
1474                if (error)
1475                        goto out_gunlock;
1476        }
1477
1478        /* Remove the target file, if it exists */
1479
1480        if (nip)
1481                error = gfs2_unlink_inode(ndip, ndentry);
1482
1483        error = update_moved_ino(ip, ndip, dir_rename);
1484        if (error)
1485                goto out_end_trans;
1486
1487        error = gfs2_dir_del(odip, odentry);
1488        if (error)
1489                goto out_end_trans;
1490
1491        error = gfs2_dir_add(ndir, &ndentry->d_name, ip);
1492        if (error)
1493                goto out_end_trans;
1494
1495out_end_trans:
1496        gfs2_trans_end(sdp);
1497out_ipreserv:
1498        if (alloc_required)
1499                gfs2_inplace_release(ndip);
1500out_gunlock_q:
1501        if (alloc_required)
1502                gfs2_quota_unlock(ndip);
1503out_gunlock:
1504        while (x--) {
1505                gfs2_glock_dq(ghs + x);
1506                gfs2_holder_uninit(ghs + x);
1507        }
1508out_gunlock_r:
1509        if (gfs2_holder_initialized(&r_gh))
1510                gfs2_glock_dq_uninit(&r_gh);
1511out:
1512        return error;
1513}
1514
1515/**
1516 * gfs2_exchange - exchange two files
1517 * @odir: Parent directory of old file name
1518 * @odentry: The old dentry of the file
1519 * @ndir: Parent directory of new file name
1520 * @ndentry: The new dentry of the file
1521 * @flags: The rename flags
1522 *
1523 * Returns: errno
1524 */
1525
1526static int gfs2_exchange(struct inode *odir, struct dentry *odentry,
1527                         struct inode *ndir, struct dentry *ndentry,
1528                         unsigned int flags)
1529{
1530        struct gfs2_inode *odip = GFS2_I(odir);
1531        struct gfs2_inode *ndip = GFS2_I(ndir);
1532        struct gfs2_inode *oip = GFS2_I(odentry->d_inode);
1533        struct gfs2_inode *nip = GFS2_I(ndentry->d_inode);
1534        struct gfs2_sbd *sdp = GFS2_SB(odir);
1535        struct gfs2_holder ghs[5], r_gh;
1536        unsigned int num_gh;
1537        unsigned int x;
1538        umode_t old_mode = oip->i_inode.i_mode;
1539        umode_t new_mode = nip->i_inode.i_mode;
1540        int error;
1541
1542        gfs2_holder_mark_uninitialized(&r_gh);
1543        error = gfs2_rindex_update(sdp);
1544        if (error)
1545                return error;
1546
1547        if (odip != ndip) {
1548                error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1549                                           0, &r_gh);
1550                if (error)
1551                        goto out;
1552
1553                if (S_ISDIR(old_mode)) {
1554                        /* don't move a directory into its subdir */
1555                        error = gfs2_ok_to_move(oip, ndip);
1556                        if (error)
1557                                goto out_gunlock_r;
1558                }
1559
1560                if (S_ISDIR(new_mode)) {
1561                        /* don't move a directory into its subdir */
1562                        error = gfs2_ok_to_move(nip, odip);
1563                        if (error)
1564                                goto out_gunlock_r;
1565                }
1566        }
1567
1568        num_gh = 1;
1569        gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
1570        if (odip != ndip) {
1571                gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
1572                num_gh++;
1573        }
1574        gfs2_holder_init(oip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
1575        num_gh++;
1576
1577        gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
1578        num_gh++;
1579
1580        for (x = 0; x < num_gh; x++) {
1581                error = gfs2_glock_nq(ghs + x);
1582                if (error)
1583                        goto out_gunlock;
1584        }
1585
1586        error = -ENOENT;
1587        if (oip->i_inode.i_nlink == 0 || nip->i_inode.i_nlink == 0)
1588                goto out_gunlock;
1589
1590        error = gfs2_unlink_ok(odip, &odentry->d_name, oip);
1591        if (error)
1592                goto out_gunlock;
1593        error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
1594        if (error)
1595                goto out_gunlock;
1596
1597        if (S_ISDIR(old_mode)) {
1598                error = gfs2_permission(odentry->d_inode, MAY_WRITE);
1599                if (error)
1600                        goto out_gunlock;
1601        }
1602        if (S_ISDIR(new_mode)) {
1603                error = gfs2_permission(ndentry->d_inode, MAY_WRITE);
1604                if (error)
1605                        goto out_gunlock;
1606        }
1607        error = gfs2_trans_begin(sdp, 4 * RES_DINODE + 4 * RES_LEAF, 0);
1608        if (error)
1609                goto out_gunlock;
1610
1611        error = update_moved_ino(oip, ndip, S_ISDIR(old_mode));
1612        if (error)
1613                goto out_end_trans;
1614
1615        error = update_moved_ino(nip, odip, S_ISDIR(new_mode));
1616        if (error)
1617                goto out_end_trans;
1618
1619        error = gfs2_dir_mvino(ndip, &ndentry->d_name, oip,
1620                               IF2DT(old_mode));
1621        if (error)
1622                goto out_end_trans;
1623
1624        error = gfs2_dir_mvino(odip, &odentry->d_name, nip,
1625                               IF2DT(new_mode));
1626        if (error)
1627                goto out_end_trans;
1628
1629        if (odip != ndip) {
1630                if (S_ISDIR(new_mode) && !S_ISDIR(old_mode)) {
1631                        inc_nlink(&odip->i_inode);
1632                        drop_nlink(&ndip->i_inode);
1633                } else if (S_ISDIR(old_mode) && !S_ISDIR(new_mode)) {
1634                        inc_nlink(&ndip->i_inode);
1635                        drop_nlink(&odip->i_inode);
1636                }
1637        }
1638        mark_inode_dirty(&ndip->i_inode);
1639        if (odip != ndip)
1640                mark_inode_dirty(&odip->i_inode);
1641
1642out_end_trans:
1643        gfs2_trans_end(sdp);
1644out_gunlock:
1645        while (x--) {
1646                gfs2_glock_dq(ghs + x);
1647                gfs2_holder_uninit(ghs + x);
1648        }
1649out_gunlock_r:
1650        if (gfs2_holder_initialized(&r_gh))
1651                gfs2_glock_dq_uninit(&r_gh);
1652out:
1653        return error;
1654}
1655
1656static int gfs2_rename2(struct inode *odir, struct dentry *odentry,
1657                        struct inode *ndir, struct dentry *ndentry,
1658                        unsigned int flags)
1659{
1660        flags &= ~RENAME_NOREPLACE;
1661
1662        if (flags & ~RENAME_EXCHANGE)
1663                return -EINVAL;
1664
1665        if (flags & RENAME_EXCHANGE)
1666                return gfs2_exchange(odir, odentry, ndir, ndentry, flags);
1667
1668        return gfs2_rename(odir, odentry, ndir, ndentry);
1669}
1670
1671/**
1672 * gfs2_follow_link - Follow a symbolic link
1673 * @dentry: The dentry of the link
1674 * @nd: Data that we pass to vfs_follow_link()
1675 *
1676 * This can handle symlinks of any size.
1677 *
1678 * Returns: 0 on success or error code
1679 */
1680
1681static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
1682{
1683        struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
1684        struct gfs2_holder i_gh;
1685        struct buffer_head *dibh;
1686        unsigned int size;
1687        char *buf;
1688        int error;
1689
1690        gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
1691        error = gfs2_glock_nq(&i_gh);
1692        if (error) {
1693                gfs2_holder_uninit(&i_gh);
1694                nd_set_link(nd, ERR_PTR(error));
1695                return NULL;
1696        }
1697
1698        size = (unsigned int)i_size_read(&ip->i_inode);
1699        if (size == 0) {
1700                gfs2_consist_inode(ip);
1701                buf = ERR_PTR(-EIO);
1702                goto out;
1703        }
1704
1705        error = gfs2_meta_inode_buffer(ip, &dibh);
1706        if (error) {
1707                buf = ERR_PTR(error);
1708                goto out;
1709        }
1710
1711        buf = kzalloc(size + 1, GFP_NOFS);
1712        if (!buf)
1713                buf = ERR_PTR(-ENOMEM);
1714        else
1715                memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
1716        brelse(dibh);
1717out:
1718        gfs2_glock_dq_uninit(&i_gh);
1719        nd_set_link(nd, buf);
1720        return NULL;
1721}
1722
1723/**
1724 * gfs2_permission -
1725 * @inode: The inode
1726 * @mask: The mask to be tested
1727 * @flags: Indicates whether this is an RCU path walk or not
1728 *
1729 * This may be called from the VFS directly, or from within GFS2 with the
1730 * inode locked, so we look to see if the glock is already locked and only
1731 * lock the glock if its not already been done.
1732 *
1733 * Returns: errno
1734 */
1735
1736int gfs2_permission(struct inode *inode, int mask)
1737{
1738        struct gfs2_inode *ip;
1739        struct gfs2_holder i_gh;
1740        int error;
1741
1742        gfs2_holder_mark_uninitialized(&i_gh);
1743        ip = GFS2_I(inode);
1744        if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1745                if (mask & MAY_NOT_BLOCK)
1746                        return -ECHILD;
1747                error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1748                if (error)
1749                        return error;
1750        }
1751
1752        if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1753                error = -EACCES;
1754        else
1755                error = generic_permission(inode, mask);
1756        if (gfs2_holder_initialized(&i_gh))
1757                gfs2_glock_dq_uninit(&i_gh);
1758
1759        return error;
1760}
1761
1762static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
1763{
1764        setattr_copy(inode, attr);
1765        mark_inode_dirty(inode);
1766        return 0;
1767}
1768
1769/**
1770 * gfs2_setattr_simple -
1771 * @ip:
1772 * @attr:
1773 *
1774 * Returns: errno
1775 */
1776
1777int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
1778{
1779        int error;
1780
1781        if (current->journal_info)
1782                return __gfs2_setattr_simple(inode, attr);
1783
1784        error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
1785        if (error)
1786                return error;
1787
1788        error = __gfs2_setattr_simple(inode, attr);
1789        gfs2_trans_end(GFS2_SB(inode));
1790        return error;
1791}
1792
1793static int setattr_chown(struct inode *inode, struct iattr *attr)
1794{
1795        struct gfs2_inode *ip = GFS2_I(inode);
1796        struct gfs2_sbd *sdp = GFS2_SB(inode);
1797        kuid_t ouid, nuid;
1798        kgid_t ogid, ngid;
1799        int error;
1800        struct gfs2_alloc_parms ap;
1801
1802        ouid = inode->i_uid;
1803        ogid = inode->i_gid;
1804        nuid = attr->ia_uid;
1805        ngid = attr->ia_gid;
1806
1807        if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid))
1808                ouid = nuid = NO_UID_QUOTA_CHANGE;
1809        if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid))
1810                ogid = ngid = NO_GID_QUOTA_CHANGE;
1811
1812        error = gfs2_rsqa_alloc(ip);
1813        if (error)
1814                goto out;
1815
1816        error = gfs2_rindex_update(sdp);
1817        if (error)
1818                goto out;
1819
1820        error = gfs2_quota_lock(ip, nuid, ngid);
1821        if (error)
1822                goto out;
1823
1824        ap.target = gfs2_get_inode_blocks(&ip->i_inode);
1825
1826        if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1827            !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
1828                error = gfs2_quota_check(ip, nuid, ngid, &ap);
1829                if (error)
1830                        goto out_gunlock_q;
1831        }
1832
1833        error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1834        if (error)
1835                goto out_gunlock_q;
1836
1837        error = gfs2_setattr_simple(inode, attr);
1838        if (error)
1839                goto out_end_trans;
1840
1841        if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1842            !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
1843                gfs2_quota_change(ip, -(s64)ap.target, ouid, ogid);
1844                gfs2_quota_change(ip, ap.target, nuid, ngid);
1845        }
1846
1847out_end_trans:
1848        gfs2_trans_end(sdp);
1849out_gunlock_q:
1850        gfs2_quota_unlock(ip);
1851out:
1852        return error;
1853}
1854
1855/**
1856 * gfs2_setattr - Change attributes on an inode
1857 * @dentry: The dentry which is changing
1858 * @attr: The structure describing the change
1859 *
1860 * The VFS layer wants to change one or more of an inodes attributes.  Write
1861 * that change out to disk.
1862 *
1863 * Returns: errno
1864 */
1865
1866static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1867{
1868        struct inode *inode = dentry->d_inode;
1869        struct gfs2_inode *ip = GFS2_I(inode);
1870        struct gfs2_holder i_gh;
1871        int error;
1872
1873        error = gfs2_rsqa_alloc(ip);
1874        if (error)
1875                return error;
1876
1877        error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1878        if (error)
1879                return error;
1880
1881        error = -EPERM;
1882        if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1883                goto out;
1884
1885        error = inode_change_ok(inode, attr);
1886        if (error)
1887                goto out;
1888
1889        if (attr->ia_valid & ATTR_SIZE)
1890                error = gfs2_setattr_size(inode, attr->ia_size);
1891        else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1892                error = setattr_chown(inode, attr);
1893        else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1894                error = gfs2_acl_chmod(ip, attr);
1895        else
1896                error = gfs2_setattr_simple(inode, attr);
1897
1898out:
1899        if (!error)
1900                mark_inode_dirty(inode);
1901        gfs2_glock_dq_uninit(&i_gh);
1902        return error;
1903}
1904
1905/**
1906 * gfs2_getattr - Read out an inode's attributes
1907 * @mnt: The vfsmount the inode is being accessed from
1908 * @dentry: The dentry to stat
1909 * @stat: The inode's stats
1910 *
1911 * This may be called from the VFS directly, or from within GFS2 with the
1912 * inode locked, so we look to see if the glock is already locked and only
1913 * lock the glock if its not already been done. Note that its the NFS
1914 * readdirplus operation which causes this to be called (from filldir)
1915 * with the glock already held.
1916 *
1917 * Returns: errno
1918 */
1919
1920static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1921                        struct kstat *stat)
1922{
1923        struct inode *inode = dentry->d_inode;
1924        struct gfs2_inode *ip = GFS2_I(inode);
1925        struct gfs2_holder gh;
1926        int error;
1927
1928        gfs2_holder_mark_uninitialized(&gh);
1929        if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1930                error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1931                if (error)
1932                        return error;
1933        }
1934
1935        generic_fillattr(inode, stat);
1936        if (gfs2_holder_initialized(&gh))
1937                gfs2_glock_dq_uninit(&gh);
1938
1939        return 0;
1940}
1941
1942static int gfs2_setxattr(struct dentry *dentry, const char *name,
1943                         const void *data, size_t size, int flags)
1944{
1945        struct inode *inode = dentry->d_inode;
1946        struct gfs2_inode *ip = GFS2_I(inode);
1947        struct gfs2_holder gh;
1948        int ret;
1949
1950        gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1951        ret = gfs2_glock_nq(&gh);
1952        if (ret == 0) {
1953                ret = gfs2_rsqa_alloc(ip);
1954                if (ret == 0)
1955                        ret = generic_setxattr(dentry, name, data, size, flags);
1956                gfs2_glock_dq(&gh);
1957        }
1958        gfs2_holder_uninit(&gh);
1959        return ret;
1960}
1961
1962static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1963                             void *data, size_t size)
1964{
1965        struct inode *inode = dentry->d_inode;
1966        struct gfs2_inode *ip = GFS2_I(inode);
1967        struct gfs2_holder gh;
1968        int ret;
1969
1970        /* For selinux during lookup */
1971        if (gfs2_glock_is_locked_by_me(ip->i_gl))
1972                return generic_getxattr(dentry, name, data, size);
1973
1974        gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1975        ret = gfs2_glock_nq(&gh);
1976        if (ret == 0) {
1977                ret = generic_getxattr(dentry, name, data, size);
1978                gfs2_glock_dq(&gh);
1979        }
1980        gfs2_holder_uninit(&gh);
1981        return ret;
1982}
1983
1984static int gfs2_removexattr(struct dentry *dentry, const char *name)
1985{
1986        struct inode *inode = dentry->d_inode;
1987        struct gfs2_inode *ip = GFS2_I(inode);
1988        struct gfs2_holder gh;
1989        int ret;
1990
1991        gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1992        ret = gfs2_glock_nq(&gh);
1993        if (ret == 0) {
1994                ret = gfs2_rsqa_alloc(ip);
1995                if (ret == 0)
1996                        ret = generic_removexattr(dentry, name);
1997                gfs2_glock_dq(&gh);
1998        }
1999        gfs2_holder_uninit(&gh);
2000        return ret;
2001}
2002
2003static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2004                       u64 start, u64 len)
2005{
2006        struct gfs2_inode *ip = GFS2_I(inode);
2007        struct gfs2_holder gh;
2008        int ret;
2009
2010        ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
2011        if (ret)
2012                return ret;
2013
2014        mutex_lock(&inode->i_mutex);
2015
2016        ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2017        if (ret)
2018                goto out;
2019
2020        if (gfs2_is_stuffed(ip)) {
2021                u64 phys = ip->i_no_addr << inode->i_blkbits;
2022                u64 size = i_size_read(inode);
2023                u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
2024                            FIEMAP_EXTENT_DATA_INLINE;
2025                phys += sizeof(struct gfs2_dinode);
2026                phys += start;
2027                if (start + len > size)
2028                        len = size - start;
2029                if (start < size)
2030                        ret = fiemap_fill_next_extent(fieinfo, start, phys,
2031                                                      len, flags);
2032                if (ret == 1)
2033                        ret = 0;
2034        } else {
2035                ret = __generic_block_fiemap(inode, fieinfo, start, len,
2036                                             gfs2_block_map);
2037        }
2038
2039        gfs2_glock_dq_uninit(&gh);
2040out:
2041        mutex_unlock(&inode->i_mutex);
2042        return ret;
2043}
2044
2045const struct inode_operations gfs2_file_iops = {
2046        .permission = gfs2_permission,
2047        .setattr = gfs2_setattr,
2048        .getattr = gfs2_getattr,
2049        .setxattr = gfs2_setxattr,
2050        .getxattr = gfs2_getxattr,
2051        .listxattr = gfs2_listxattr,
2052        .removexattr = gfs2_removexattr,
2053        .fiemap = gfs2_fiemap,
2054        .get_acl = gfs2_get_acl,
2055};
2056
2057const struct inode_operations_wrapper gfs2_dir_iops = {
2058        .ops = {
2059        .create = gfs2_create,
2060        .lookup = gfs2_lookup,
2061        .link = gfs2_link,
2062        .unlink = gfs2_unlink,
2063        .symlink = gfs2_symlink,
2064        .mkdir = gfs2_mkdir,
2065        .rmdir = gfs2_unlink,
2066        .mknod = gfs2_mknod,
2067        .rename = gfs2_rename,
2068        .permission = gfs2_permission,
2069        .setattr = gfs2_setattr,
2070        .getattr = gfs2_getattr,
2071        .setxattr = gfs2_setxattr,
2072        .getxattr = gfs2_getxattr,
2073        .listxattr = gfs2_listxattr,
2074        .removexattr = gfs2_removexattr,
2075        .fiemap = gfs2_fiemap,
2076        .get_acl = gfs2_get_acl,
2077        .atomic_open = gfs2_atomic_open,
2078        },
2079        .rename2 = gfs2_rename2,
2080};
2081
2082const struct inode_operations gfs2_symlink_iops = {
2083        .readlink = generic_readlink,
2084        .follow_link = gfs2_follow_link,
2085        .put_link = kfree_put_link,
2086        .permission = gfs2_permission,
2087        .setattr = gfs2_setattr,
2088        .getattr = gfs2_getattr,
2089        .setxattr = gfs2_setxattr,
2090        .getxattr = gfs2_getxattr,
2091        .listxattr = gfs2_listxattr,
2092        .removexattr = gfs2_removexattr,
2093        .fiemap = gfs2_fiemap,
2094        .get_acl = gfs2_get_acl,
2095};
2096
2097