linux/drivers/staging/lustre/lustre/llite/namei.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * GPL HEADER START
   4 *
   5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License version 2 only,
   9 * as published by the Free Software Foundation.
  10 *
  11 * This program is distributed in the hope that it will be useful, but
  12 * WITHOUT ANY WARRANTY; without even the implied warranty of
  13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14 * General Public License version 2 for more details (a copy is included
  15 * in the LICENSE file that accompanied this code).
  16 *
  17 * You should have received a copy of the GNU General Public License
  18 * version 2 along with this program; If not, see
  19 * http://www.gnu.org/licenses/gpl-2.0.html
  20 *
  21 * GPL HEADER END
  22 */
  23/*
  24 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
  25 * Use is subject to license terms.
  26 *
  27 * Copyright (c) 2011, 2015, Intel Corporation.
  28 */
  29/*
  30 * This file is part of Lustre, http://www.lustre.org/
  31 * Lustre is a trademark of Sun Microsystems, Inc.
  32 */
  33
  34#include <linux/fs.h>
  35#include <linux/sched.h>
  36#include <linux/mm.h>
  37#include <linux/quotaops.h>
  38#include <linux/highmem.h>
  39#include <linux/pagemap.h>
  40#include <linux/security.h>
  41
  42#define DEBUG_SUBSYSTEM S_LLITE
  43
  44#include <obd_support.h>
  45#include <lustre_fid.h>
  46#include <lustre_dlm.h>
  47#include "llite_internal.h"
  48
  49static int ll_create_it(struct inode *dir, struct dentry *dentry,
  50                        struct lookup_intent *it);
  51
  52/* called from iget5_locked->find_inode() under inode_hash_lock spinlock */
  53static int ll_test_inode(struct inode *inode, void *opaque)
  54{
  55        struct ll_inode_info *lli = ll_i2info(inode);
  56        struct lustre_md     *md = opaque;
  57
  58        if (unlikely(!(md->body->mbo_valid & OBD_MD_FLID))) {
  59                CERROR("MDS body missing FID\n");
  60                return 0;
  61        }
  62
  63        if (!lu_fid_eq(&lli->lli_fid, &md->body->mbo_fid1))
  64                return 0;
  65
  66        return 1;
  67}
  68
  69static int ll_set_inode(struct inode *inode, void *opaque)
  70{
  71        struct ll_inode_info *lli = ll_i2info(inode);
  72        struct mdt_body *body = ((struct lustre_md *)opaque)->body;
  73
  74        if (unlikely(!(body->mbo_valid & OBD_MD_FLID))) {
  75                CERROR("MDS body missing FID\n");
  76                return -EINVAL;
  77        }
  78
  79        lli->lli_fid = body->mbo_fid1;
  80        if (unlikely(!(body->mbo_valid & OBD_MD_FLTYPE))) {
  81                CERROR("Can not initialize inode " DFID
  82                       " without object type: valid = %#llx\n",
  83                       PFID(&lli->lli_fid), body->mbo_valid);
  84                return -EINVAL;
  85        }
  86
  87        inode->i_mode = (inode->i_mode & ~S_IFMT) | (body->mbo_mode & S_IFMT);
  88        if (unlikely(inode->i_mode == 0)) {
  89                CERROR("Invalid inode " DFID " type\n", PFID(&lli->lli_fid));
  90                return -EINVAL;
  91        }
  92
  93        ll_lli_init(lli);
  94
  95        return 0;
  96}
  97
  98/**
  99 * Get an inode by inode number(@hash), which is already instantiated by
 100 * the intent lookup).
 101 */
 102struct inode *ll_iget(struct super_block *sb, ino_t hash,
 103                      struct lustre_md *md)
 104{
 105        struct inode     *inode;
 106        int rc = 0;
 107
 108        LASSERT(hash != 0);
 109        inode = iget5_locked(sb, hash, ll_test_inode, ll_set_inode, md);
 110        if (!inode)
 111                return ERR_PTR(-ENOMEM);
 112
 113        if (inode->i_state & I_NEW) {
 114                rc = ll_read_inode2(inode, md);
 115                if (!rc && S_ISREG(inode->i_mode) &&
 116                    !ll_i2info(inode)->lli_clob)
 117                        rc = cl_file_inode_init(inode, md);
 118
 119                if (rc) {
 120                        /*
 121                         * Let's clear directory lsm here, otherwise
 122                         * make_bad_inode() will reset the inode mode
 123                         * to regular, then ll_clear_inode will not
 124                         * be able to clear lsm_md
 125                         */
 126                        if (S_ISDIR(inode->i_mode))
 127                                ll_dir_clear_lsm_md(inode);
 128                        make_bad_inode(inode);
 129                        unlock_new_inode(inode);
 130                        iput(inode);
 131                        inode = ERR_PTR(rc);
 132                } else {
 133                        unlock_new_inode(inode);
 134                }
 135        } else if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
 136                rc = ll_update_inode(inode, md);
 137                CDEBUG(D_VFSTRACE, "got inode: " DFID "(%p): rc = %d\n",
 138                       PFID(&md->body->mbo_fid1), inode, rc);
 139                if (rc) {
 140                        if (S_ISDIR(inode->i_mode))
 141                                ll_dir_clear_lsm_md(inode);
 142                        iput(inode);
 143                        inode = ERR_PTR(rc);
 144                }
 145        }
 146        return inode;
 147}
 148
 149static void ll_invalidate_negative_children(struct inode *dir)
 150{
 151        struct dentry *dentry, *tmp_subdir;
 152
 153        spin_lock(&dir->i_lock);
 154        hlist_for_each_entry(dentry, &dir->i_dentry, d_u.d_alias) {
 155                spin_lock(&dentry->d_lock);
 156                if (!list_empty(&dentry->d_subdirs)) {
 157                        struct dentry *child;
 158
 159                        list_for_each_entry_safe(child, tmp_subdir,
 160                                                 &dentry->d_subdirs,
 161                                                 d_child) {
 162                                if (d_really_is_negative(child))
 163                                        d_lustre_invalidate(child, 1);
 164                        }
 165                }
 166                spin_unlock(&dentry->d_lock);
 167        }
 168        spin_unlock(&dir->i_lock);
 169}
 170
 171int ll_test_inode_by_fid(struct inode *inode, void *opaque)
 172{
 173        return lu_fid_eq(&ll_i2info(inode)->lli_fid, opaque);
 174}
 175
 176int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
 177                       void *data, int flag)
 178{
 179        struct lustre_handle lockh;
 180        int rc;
 181
 182        switch (flag) {
 183        case LDLM_CB_BLOCKING:
 184                ldlm_lock2handle(lock, &lockh);
 185                rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
 186                if (rc < 0) {
 187                        CDEBUG(D_INODE, "ldlm_cli_cancel: rc = %d\n", rc);
 188                        return rc;
 189                }
 190                break;
 191        case LDLM_CB_CANCELING: {
 192                struct inode *inode = ll_inode_from_resource_lock(lock);
 193                __u64 bits = lock->l_policy_data.l_inodebits.bits;
 194
 195                /* Inode is set to lock->l_resource->lr_lvb_inode
 196                 * for mdc - bug 24555
 197                 */
 198                LASSERT(!lock->l_ast_data);
 199
 200                if (!inode)
 201                        break;
 202
 203                /* Invalidate all dentries associated with this inode */
 204                LASSERT(ldlm_is_canceling(lock));
 205
 206                if (!fid_res_name_eq(ll_inode2fid(inode),
 207                                     &lock->l_resource->lr_name)) {
 208                        LDLM_ERROR(lock,
 209                                   "data mismatch with object " DFID "(%p)",
 210                                   PFID(ll_inode2fid(inode)), inode);
 211                        LBUG();
 212                }
 213
 214                if (bits & MDS_INODELOCK_XATTR) {
 215                        if (S_ISDIR(inode->i_mode))
 216                                ll_i2info(inode)->lli_def_stripe_offset = -1;
 217                        ll_xattr_cache_destroy(inode);
 218                        bits &= ~MDS_INODELOCK_XATTR;
 219                }
 220
 221                /* For OPEN locks we differentiate between lock modes
 222                 * LCK_CR, LCK_CW, LCK_PR - bug 22891
 223                 */
 224                if (bits & MDS_INODELOCK_OPEN)
 225                        ll_have_md_lock(inode, &bits, lock->l_req_mode);
 226
 227                if (bits & MDS_INODELOCK_OPEN) {
 228                        fmode_t fmode;
 229
 230                        switch (lock->l_req_mode) {
 231                        case LCK_CW:
 232                                fmode = FMODE_WRITE;
 233                                break;
 234                        case LCK_PR:
 235                                fmode = FMODE_EXEC;
 236                                break;
 237                        case LCK_CR:
 238                                fmode = FMODE_READ;
 239                                break;
 240                        default:
 241                                LDLM_ERROR(lock, "bad lock mode for OPEN lock");
 242                                LBUG();
 243                        }
 244
 245                        ll_md_real_close(inode, fmode);
 246                }
 247
 248                if (bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
 249                            MDS_INODELOCK_LAYOUT | MDS_INODELOCK_PERM))
 250                        ll_have_md_lock(inode, &bits, LCK_MINMODE);
 251
 252                if (bits & MDS_INODELOCK_LAYOUT) {
 253                        struct cl_object_conf conf = {
 254                                .coc_opc = OBJECT_CONF_INVALIDATE,
 255                                .coc_inode = inode,
 256                        };
 257
 258                        rc = ll_layout_conf(inode, &conf);
 259                        if (rc < 0)
 260                                CDEBUG(D_INODE, "cannot invalidate layout of "
 261                                       DFID ": rc = %d\n",
 262                                       PFID(ll_inode2fid(inode)), rc);
 263                }
 264
 265                if (bits & MDS_INODELOCK_UPDATE) {
 266                        struct ll_inode_info *lli = ll_i2info(inode);
 267
 268                        spin_lock(&lli->lli_lock);
 269                        LTIME_S(inode->i_mtime) = 0;
 270                        LTIME_S(inode->i_atime) = 0;
 271                        LTIME_S(inode->i_ctime) = 0;
 272                        spin_unlock(&lli->lli_lock);
 273                }
 274
 275                if ((bits & MDS_INODELOCK_UPDATE) && S_ISDIR(inode->i_mode)) {
 276                        struct ll_inode_info *lli = ll_i2info(inode);
 277
 278                        CDEBUG(D_INODE, "invalidating inode " DFID " lli = %p, pfid  = " DFID "\n",
 279                               PFID(ll_inode2fid(inode)), lli,
 280                               PFID(&lli->lli_pfid));
 281
 282                        truncate_inode_pages(inode->i_mapping, 0);
 283
 284                        if (unlikely(!fid_is_zero(&lli->lli_pfid))) {
 285                                struct inode *master_inode = NULL;
 286                                unsigned long hash;
 287
 288                                /*
 289                                 * This is slave inode, since all of the child
 290                                 * dentry is connected on the master inode, so
 291                                 * we have to invalidate the negative children
 292                                 * on master inode
 293                                 */
 294                                CDEBUG(D_INODE,
 295                                       "Invalidate s" DFID " m" DFID "\n",
 296                                       PFID(ll_inode2fid(inode)),
 297                                       PFID(&lli->lli_pfid));
 298
 299                                hash = cl_fid_build_ino(&lli->lli_pfid,
 300                                                        ll_need_32bit_api(ll_i2sbi(inode)));
 301                                /*
 302                                 * Do not lookup the inode with ilookup5,
 303                                 * otherwise it will cause dead lock,
 304                                 *
 305                                 * 1. Client1 send chmod req to the MDT0, then
 306                                 * on MDT0, it enqueues master and all of its
 307                                 * slaves lock, (mdt_attr_set() ->
 308                                 * mdt_lock_slaves()), after gets master and
 309                                 * stripe0 lock, it will send the enqueue req
 310                                 * (for stripe1) to MDT1, then MDT1 finds the
 311                                 * lock has been granted to client2. Then MDT1
 312                                 * sends blocking ast to client2.
 313                                 *
 314                                 * 2. At the same time, client2 tries to unlink
 315                                 * the striped dir (rm -rf striped_dir), and
 316                                 * during lookup, it will hold the master inode
 317                                 * of the striped directory, whose inode state
 318                                 * is NEW, then tries to revalidate all of its
 319                                 * slaves, (ll_prep_inode()->ll_iget()->
 320                                 * ll_read_inode2()-> ll_update_inode().). And
 321                                 * it will be blocked on the server side because
 322                                 * of 1.
 323                                 *
 324                                 * 3. Then the client get the blocking_ast req,
 325                                 * cancel the lock, but being blocked if using
 326                                 * ->ilookup5()), because master inode state is
 327                                 *  NEW.
 328                                 */
 329                                master_inode = ilookup5_nowait(inode->i_sb,
 330                                                               hash,
 331                                                               ll_test_inode_by_fid,
 332                                                               (void *)&lli->lli_pfid);
 333                                if (master_inode) {
 334                                        ll_invalidate_negative_children(master_inode);
 335                                        iput(master_inode);
 336                                }
 337                        } else {
 338                                ll_invalidate_negative_children(inode);
 339                        }
 340                }
 341
 342                if ((bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM)) &&
 343                    inode->i_sb->s_root &&
 344                    !is_root_inode(inode))
 345                        ll_invalidate_aliases(inode);
 346
 347                iput(inode);
 348                break;
 349        }
 350        default:
 351                LBUG();
 352        }
 353
 354        return 0;
 355}
 356
 357__u32 ll_i2suppgid(struct inode *i)
 358{
 359        if (in_group_p(i->i_gid))
 360                return (__u32)from_kgid(&init_user_ns, i->i_gid);
 361        else
 362                return (__u32)(-1);
 363}
 364
 365/* Pack the required supplementary groups into the supplied groups array.
 366 * If we don't need to use the groups from the target inode(s) then we
 367 * instead pack one or more groups from the user's supplementary group
 368 * array in case it might be useful.  Not needed if doing an MDS-side upcall.
 369 */
 370void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
 371{
 372        LASSERT(i1);
 373
 374        suppgids[0] = ll_i2suppgid(i1);
 375
 376        if (i2)
 377                suppgids[1] = ll_i2suppgid(i2);
 378                else
 379                        suppgids[1] = -1;
 380}
 381
 382/*
 383 * try to reuse three types of dentry:
 384 * 1. unhashed alias, this one is unhashed by d_invalidate (but it may be valid
 385 *    by concurrent .revalidate).
 386 * 2. INVALID alias (common case for no valid ldlm lock held, but this flag may
 387 *    be cleared by others calling d_lustre_revalidate).
 388 * 3. DISCONNECTED alias.
 389 */
 390static struct dentry *ll_find_alias(struct inode *inode, struct dentry *dentry)
 391{
 392        struct dentry *alias, *discon_alias, *invalid_alias;
 393
 394        if (hlist_empty(&inode->i_dentry))
 395                return NULL;
 396
 397        discon_alias = NULL;
 398        invalid_alias = NULL;
 399
 400        spin_lock(&inode->i_lock);
 401        hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
 402                LASSERT(alias != dentry);
 403
 404                spin_lock(&alias->d_lock);
 405                if ((alias->d_flags & DCACHE_DISCONNECTED) &&
 406                    S_ISDIR(inode->i_mode))
 407                        /* LASSERT(last_discon == NULL); LU-405, bz 20055 */
 408                        discon_alias = alias;
 409                else if (alias->d_parent == dentry->d_parent         &&
 410                         alias->d_name.hash == dentry->d_name.hash       &&
 411                         alias->d_name.len == dentry->d_name.len         &&
 412                         memcmp(alias->d_name.name, dentry->d_name.name,
 413                                dentry->d_name.len) == 0)
 414                        invalid_alias = alias;
 415                spin_unlock(&alias->d_lock);
 416
 417                if (invalid_alias)
 418                        break;
 419        }
 420        alias = invalid_alias ?: discon_alias ?: NULL;
 421        if (alias) {
 422                spin_lock(&alias->d_lock);
 423                dget_dlock(alias);
 424                spin_unlock(&alias->d_lock);
 425        }
 426        spin_unlock(&inode->i_lock);
 427
 428        return alias;
 429}
 430
 431/*
 432 * Similar to d_splice_alias(), but lustre treats invalid alias
 433 * similar to DCACHE_DISCONNECTED, and tries to use it anyway.
 434 */
 435struct dentry *ll_splice_alias(struct inode *inode, struct dentry *de)
 436{
 437        if (inode) {
 438                struct dentry *new = ll_find_alias(inode, de);
 439
 440                if (new) {
 441                        d_move(new, de);
 442                        iput(inode);
 443                        CDEBUG(D_DENTRY,
 444                               "Reuse dentry %p inode %p refc %d flags %#x\n",
 445                              new, d_inode(new), d_count(new), new->d_flags);
 446                        return new;
 447                }
 448        }
 449        d_add(de, inode);
 450        CDEBUG(D_DENTRY, "Add dentry %p inode %p refc %d flags %#x\n",
 451               de, d_inode(de), d_count(de), de->d_flags);
 452        return de;
 453}
 454
 455static int ll_lookup_it_finish(struct ptlrpc_request *request,
 456                               struct lookup_intent *it,
 457                               struct inode *parent, struct dentry **de)
 458{
 459        struct inode *inode = NULL;
 460        __u64 bits = 0;
 461        int rc = 0;
 462        struct dentry *alias;
 463
 464        /* NB 1 request reference will be taken away by ll_intent_lock()
 465         * when I return
 466         */
 467        CDEBUG(D_DENTRY, "it %p it_disposition %x\n", it,
 468               it->it_disposition);
 469        if (!it_disposition(it, DISP_LOOKUP_NEG)) {
 470                rc = ll_prep_inode(&inode, request, (*de)->d_sb, it);
 471                if (rc)
 472                        return rc;
 473
 474                ll_set_lock_data(ll_i2sbi(parent)->ll_md_exp, inode, it, &bits);
 475
 476                /* We used to query real size from OSTs here, but actually
 477                 * this is not needed. For stat() calls size would be updated
 478                 * from subsequent do_revalidate()->ll_inode_revalidate_it() in
 479                 * 2.4 and
 480                 * vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
 481                 * Everybody else who needs correct file size would call
 482                 * ll_glimpse_size or some equivalent themselves anyway.
 483                 * Also see bug 7198.
 484                 */
 485        }
 486
 487        alias = ll_splice_alias(inode, *de);
 488        if (IS_ERR(alias)) {
 489                rc = PTR_ERR(alias);
 490                goto out;
 491        }
 492        *de = alias;
 493
 494        if (!it_disposition(it, DISP_LOOKUP_NEG)) {
 495                /* We have the "lookup" lock, so unhide dentry */
 496                if (bits & MDS_INODELOCK_LOOKUP)
 497                        d_lustre_revalidate(*de);
 498        } else if (!it_disposition(it, DISP_OPEN_CREATE)) {
 499                /* If file created on server, don't depend on parent UPDATE
 500                 * lock to unhide it. It is left hidden and next lookup can
 501                 * find it in ll_splice_alias.
 502                 */
 503                /* Check that parent has UPDATE lock. */
 504                struct lookup_intent parent_it = {
 505                                        .it_op = IT_GETATTR,
 506                                        .it_lock_handle = 0 };
 507                struct lu_fid fid = ll_i2info(parent)->lli_fid;
 508
 509                /* If it is striped directory, get the real stripe parent */
 510                if (unlikely(ll_i2info(parent)->lli_lsm_md)) {
 511                        rc = md_get_fid_from_lsm(ll_i2mdexp(parent),
 512                                                 ll_i2info(parent)->lli_lsm_md,
 513                                                 (*de)->d_name.name,
 514                                                 (*de)->d_name.len, &fid);
 515                        if (rc)
 516                                return rc;
 517                }
 518
 519                if (md_revalidate_lock(ll_i2mdexp(parent), &parent_it, &fid,
 520                                       NULL)) {
 521                        d_lustre_revalidate(*de);
 522                        ll_intent_release(&parent_it);
 523                }
 524        }
 525
 526out:
 527        if (rc != 0 && it->it_op & IT_OPEN)
 528                ll_open_cleanup((*de)->d_sb, request);
 529
 530        return rc;
 531}
 532
 533static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
 534                                   struct lookup_intent *it)
 535{
 536        struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
 537        struct dentry *save = dentry, *retval;
 538        struct ptlrpc_request *req = NULL;
 539        struct md_op_data *op_data = NULL;
 540        struct inode *inode;
 541        __u32 opc;
 542        int rc;
 543
 544        if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
 545                return ERR_PTR(-ENAMETOOLONG);
 546
 547        CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir=" DFID "(%p),intent=%s\n",
 548               dentry, PFID(ll_inode2fid(parent)), parent, LL_IT2STR(it));
 549
 550        if (d_mountpoint(dentry))
 551                CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
 552
 553        if (!it || it->it_op == IT_GETXATTR)
 554                it = &lookup_it;
 555
 556        if (it->it_op == IT_GETATTR && dentry_may_statahead(parent, dentry)) {
 557                rc = ll_statahead(parent, &dentry, 0);
 558                if (rc == 1) {
 559                        if (dentry == save)
 560                                retval = NULL;
 561                        else
 562                                retval = dentry;
 563                        goto out;
 564                }
 565        }
 566
 567        if (it->it_op & IT_OPEN && it->it_flags & FMODE_WRITE && sb_rdonly(dentry->d_sb))
 568                return ERR_PTR(-EROFS);
 569
 570        if (it->it_op & IT_CREAT)
 571                opc = LUSTRE_OPC_CREATE;
 572        else
 573                opc = LUSTRE_OPC_ANY;
 574
 575        op_data = ll_prep_md_op_data(NULL, parent, NULL, dentry->d_name.name,
 576                                     dentry->d_name.len, 0, opc, NULL);
 577        if (IS_ERR(op_data))
 578                return (void *)op_data;
 579
 580        /* enforce umask if acl disabled or MDS doesn't support umask */
 581        if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
 582                it->it_create_mode &= ~current_umask();
 583
 584        rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
 585                            &ll_md_blocking_ast, 0);
 586        /*
 587         * If the MDS allows the client to chgrp (CFS_SETGRP_PERM), but the
 588         * client does not know which suppgid should be sent to the MDS, or
 589         * some other(s) changed the target file's GID after this RPC sent
 590         * to the MDS with the suppgid as the original GID, then we should
 591         * try again with right suppgid.
 592         */
 593        if (rc == -EACCES && it->it_op & IT_OPEN &&
 594            it_disposition(it, DISP_OPEN_DENY)) {
 595                struct mdt_body *body;
 596
 597                LASSERT(req);
 598
 599                body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
 600                if (op_data->op_suppgids[0] == body->mbo_gid ||
 601                    op_data->op_suppgids[1] == body->mbo_gid ||
 602                    !in_group_p(make_kgid(&init_user_ns, body->mbo_gid))) {
 603                        retval = ERR_PTR(-EACCES);
 604                        goto out;
 605                }
 606
 607                fid_zero(&op_data->op_fid2);
 608                op_data->op_suppgids[1] = body->mbo_gid;
 609                ptlrpc_req_finished(req);
 610                req = NULL;
 611                ll_intent_release(it);
 612                rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
 613                                    ll_md_blocking_ast, 0);
 614        }
 615
 616        if (rc < 0) {
 617                retval = ERR_PTR(rc);
 618                goto out;
 619        }
 620
 621        rc = ll_lookup_it_finish(req, it, parent, &dentry);
 622        if (rc != 0) {
 623                ll_intent_release(it);
 624                retval = ERR_PTR(rc);
 625                goto out;
 626        }
 627
 628        inode = d_inode(dentry);
 629        if ((it->it_op & IT_OPEN) && inode &&
 630            !S_ISREG(inode->i_mode) &&
 631            !S_ISDIR(inode->i_mode)) {
 632                ll_release_openhandle(inode, it);
 633        }
 634        ll_lookup_finish_locks(it, inode);
 635
 636        if (dentry == save)
 637                retval = NULL;
 638        else
 639                retval = dentry;
 640out:
 641        if (op_data && !IS_ERR(op_data))
 642                ll_finish_md_op_data(op_data);
 643
 644        ptlrpc_req_finished(req);
 645        return retval;
 646}
 647
 648static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
 649                                   unsigned int flags)
 650{
 651        struct lookup_intent *itp, it = { .it_op = IT_GETATTR };
 652        struct dentry *de;
 653
 654        CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir=" DFID "(%p),flags=%u\n",
 655               dentry, PFID(ll_inode2fid(parent)), parent, flags);
 656
 657        /* Optimize away (CREATE && !OPEN). Let .create handle the race.
 658         * but only if we have write permissions there, otherwise we need
 659         * to proceed with lookup. LU-4185
 660         */
 661        if ((flags & LOOKUP_CREATE) && !(flags & LOOKUP_OPEN) &&
 662            (inode_permission(parent, MAY_WRITE | MAY_EXEC) == 0))
 663                return NULL;
 664
 665        if (flags & (LOOKUP_PARENT | LOOKUP_OPEN | LOOKUP_CREATE))
 666                itp = NULL;
 667        else
 668                itp = &it;
 669        de = ll_lookup_it(parent, dentry, itp);
 670
 671        if (itp)
 672                ll_intent_release(itp);
 673
 674        return de;
 675}
 676
 677/*
 678 * For cached negative dentry and new dentry, handle lookup/create/open
 679 * together.
 680 */
 681static int ll_atomic_open(struct inode *dir, struct dentry *dentry,
 682                          struct file *file, unsigned int open_flags,
 683                          umode_t mode, int *opened)
 684{
 685        struct lookup_intent *it;
 686        struct dentry *de;
 687        int rc = 0;
 688
 689        CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir=" DFID "(%p),file %p,open_flags %x,mode %x opened %d\n",
 690               dentry, PFID(ll_inode2fid(dir)), dir, file, open_flags, mode,
 691               *opened);
 692
 693        /* Only negative dentries enter here */
 694        LASSERT(!d_inode(dentry));
 695
 696        if (!d_in_lookup(dentry)) {
 697                /* A valid negative dentry that just passed revalidation,
 698                 * there's little point to try and open it server-side,
 699                 * even though there's a minuscle chance it might succeed.
 700                 * Either way it's a valid race to just return -ENOENT here.
 701                 */
 702                if (!(open_flags & O_CREAT))
 703                        return -ENOENT;
 704
 705                /* Otherwise we just unhash it to be rehashed afresh via
 706                 * lookup if necessary
 707                 */
 708                d_drop(dentry);
 709        }
 710
 711        it = kzalloc(sizeof(*it), GFP_NOFS);
 712        if (!it)
 713                return -ENOMEM;
 714
 715        it->it_op = IT_OPEN;
 716        if (open_flags & O_CREAT)
 717                it->it_op |= IT_CREAT;
 718        it->it_create_mode = (mode & S_IALLUGO) | S_IFREG;
 719        it->it_flags = (open_flags & ~O_ACCMODE) | OPEN_FMODE(open_flags);
 720        it->it_flags &= ~MDS_OPEN_FL_INTERNAL;
 721
 722        /* Dentry added to dcache tree in ll_lookup_it */
 723        de = ll_lookup_it(dir, dentry, it);
 724        if (IS_ERR(de))
 725                rc = PTR_ERR(de);
 726        else if (de)
 727                dentry = de;
 728
 729        if (!rc) {
 730                if (it_disposition(it, DISP_OPEN_CREATE)) {
 731                        /* Dentry instantiated in ll_create_it. */
 732                        rc = ll_create_it(dir, dentry, it);
 733                        if (rc) {
 734                                /* We dget in ll_splice_alias. */
 735                                if (de)
 736                                        dput(de);
 737                                goto out_release;
 738                        }
 739
 740                        *opened |= FILE_CREATED;
 741                }
 742                if (d_really_is_positive(dentry) &&
 743                    it_disposition(it, DISP_OPEN_OPEN)) {
 744                        /* Open dentry. */
 745                        if (S_ISFIFO(d_inode(dentry)->i_mode)) {
 746                                /* We cannot call open here as it might
 747                                 * deadlock. This case is unreachable in
 748                                 * practice because of OBD_CONNECT_NODEVOH.
 749                                 */
 750                                rc = finish_no_open(file, de);
 751                        } else {
 752                                file->private_data = it;
 753                                rc = finish_open(file, dentry, NULL, opened);
 754                                /* We dget in ll_splice_alias. finish_open takes
 755                                 * care of dget for fd open.
 756                                 */
 757                                if (de)
 758                                        dput(de);
 759                        }
 760                } else {
 761                        rc = finish_no_open(file, de);
 762                }
 763        }
 764
 765out_release:
 766        ll_intent_release(it);
 767        kfree(it);
 768
 769        return rc;
 770}
 771
 772/* We depend on "mode" being set with the proper file type/umask by now */
 773static struct inode *ll_create_node(struct inode *dir, struct lookup_intent *it)
 774{
 775        struct inode *inode = NULL;
 776        struct ptlrpc_request *request = NULL;
 777        struct ll_sb_info *sbi = ll_i2sbi(dir);
 778        int rc;
 779
 780        LASSERT(it && it->it_disposition);
 781
 782        LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
 783        request = it->it_request;
 784        it_clear_disposition(it, DISP_ENQ_CREATE_REF);
 785        rc = ll_prep_inode(&inode, request, dir->i_sb, it);
 786        if (rc) {
 787                inode = ERR_PTR(rc);
 788                goto out;
 789        }
 790
 791        LASSERT(hlist_empty(&inode->i_dentry));
 792
 793        /* We asked for a lock on the directory, but were granted a
 794         * lock on the inode.  Since we finally have an inode pointer,
 795         * stuff it in the lock.
 796         */
 797        CDEBUG(D_DLMTRACE, "setting l_ast_data to inode " DFID "(%p)\n",
 798               PFID(ll_inode2fid(dir)), inode);
 799        ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
 800 out:
 801        ptlrpc_req_finished(request);
 802        return inode;
 803}
 804
 805/*
 806 * By the time this is called, we already have created the directory cache
 807 * entry for the new file, but it is so far negative - it has no inode.
 808 *
 809 * We defer creating the OBD object(s) until open, to keep the intent and
 810 * non-intent code paths similar, and also because we do not have the MDS
 811 * inode number before calling ll_create_node() (which is needed for LOV),
 812 * so we would need to do yet another RPC to the MDS to store the LOV EA
 813 * data on the MDS.  If needed, we would pass the PACKED lmm as data and
 814 * lmm_size in datalen (the MDS still has code which will handle that).
 815 *
 816 * If the create succeeds, we fill in the inode information
 817 * with d_instantiate().
 818 */
 819static int ll_create_it(struct inode *dir, struct dentry *dentry,
 820                        struct lookup_intent *it)
 821{
 822        struct inode *inode;
 823        int rc = 0;
 824
 825        CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir=" DFID "(%p), intent=%s\n",
 826               dentry, PFID(ll_inode2fid(dir)), dir, LL_IT2STR(it));
 827
 828        rc = it_open_error(DISP_OPEN_CREATE, it);
 829        if (rc)
 830                return rc;
 831
 832        inode = ll_create_node(dir, it);
 833        if (IS_ERR(inode))
 834                return PTR_ERR(inode);
 835
 836        d_instantiate(dentry, inode);
 837
 838        return ll_init_security(dentry, inode, dir);
 839}
 840
 841void ll_update_times(struct ptlrpc_request *request, struct inode *inode)
 842{
 843        struct mdt_body *body = req_capsule_server_get(&request->rq_pill,
 844                                                       &RMF_MDT_BODY);
 845
 846        LASSERT(body);
 847        if (body->mbo_valid & OBD_MD_FLMTIME &&
 848            body->mbo_mtime > LTIME_S(inode->i_mtime)) {
 849                CDEBUG(D_INODE, "setting fid " DFID " mtime from %lu to %llu\n",
 850                       PFID(ll_inode2fid(inode)), LTIME_S(inode->i_mtime),
 851                       body->mbo_mtime);
 852                LTIME_S(inode->i_mtime) = body->mbo_mtime;
 853        }
 854        if (body->mbo_valid & OBD_MD_FLCTIME &&
 855            body->mbo_ctime > LTIME_S(inode->i_ctime))
 856                LTIME_S(inode->i_ctime) = body->mbo_ctime;
 857}
 858
 859static int ll_new_node(struct inode *dir, struct dentry *dentry,
 860                       const char *tgt, umode_t mode, int rdev,
 861                       __u32 opc)
 862{
 863        struct ptlrpc_request *request = NULL;
 864        struct md_op_data *op_data;
 865        struct inode *inode = NULL;
 866        struct ll_sb_info *sbi = ll_i2sbi(dir);
 867        int tgt_len = 0;
 868        int err;
 869
 870        if (unlikely(tgt))
 871                tgt_len = strlen(tgt) + 1;
 872again:
 873        op_data = ll_prep_md_op_data(NULL, dir, NULL,
 874                                     dentry->d_name.name,
 875                                     dentry->d_name.len,
 876                                     0, opc, NULL);
 877        if (IS_ERR(op_data)) {
 878                err = PTR_ERR(op_data);
 879                goto err_exit;
 880        }
 881
 882        err = md_create(sbi->ll_md_exp, op_data, tgt, tgt_len, mode,
 883                        from_kuid(&init_user_ns, current_fsuid()),
 884                        from_kgid(&init_user_ns, current_fsgid()),
 885                        cfs_curproc_cap_pack(), rdev, &request);
 886        ll_finish_md_op_data(op_data);
 887        if (err < 0 && err != -EREMOTE)
 888                goto err_exit;
 889
 890        /*
 891         * If the client doesn't know where to create a subdirectory (or
 892         * in case of a race that sends the RPC to the wrong MDS), the
 893         * MDS will return -EREMOTE and the client will fetch the layout
 894         * of the directory, then create the directory on the right MDT.
 895         */
 896        if (unlikely(err == -EREMOTE)) {
 897                struct ll_inode_info *lli = ll_i2info(dir);
 898                struct lmv_user_md *lum;
 899                int lumsize, err2;
 900
 901                ptlrpc_req_finished(request);
 902                request = NULL;
 903
 904                err2 = ll_dir_getstripe(dir, (void **)&lum, &lumsize, &request,
 905                                        OBD_MD_DEFAULT_MEA);
 906                if (!err2) {
 907                        /* Update stripe_offset and retry */
 908                        lli->lli_def_stripe_offset = lum->lum_stripe_offset;
 909                } else if (err2 == -ENODATA &&
 910                           lli->lli_def_stripe_offset != -1) {
 911                        /*
 912                         * If there are no default stripe EA on the MDT, but the
 913                         * client has default stripe, then it probably means
 914                         * default stripe EA has just been deleted.
 915                         */
 916                        lli->lli_def_stripe_offset = -1;
 917                } else {
 918                        goto err_exit;
 919                }
 920
 921                ptlrpc_req_finished(request);
 922                request = NULL;
 923                goto again;
 924        }
 925
 926        ll_update_times(request, dir);
 927
 928        err = ll_prep_inode(&inode, request, dir->i_sb, NULL);
 929        if (err)
 930                goto err_exit;
 931
 932        d_instantiate(dentry, inode);
 933
 934        err = ll_init_security(dentry, inode, dir);
 935err_exit:
 936        if (request)
 937                ptlrpc_req_finished(request);
 938
 939        return err;
 940}
 941
 942static int ll_mknod(struct inode *dir, struct dentry *dchild,
 943                    umode_t mode, dev_t rdev)
 944{
 945        int err;
 946
 947        CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir=" DFID "(%p) mode %o dev %x\n",
 948               dchild, PFID(ll_inode2fid(dir)), dir, mode,
 949               old_encode_dev(rdev));
 950
 951        if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
 952                mode &= ~current_umask();
 953
 954        switch (mode & S_IFMT) {
 955        case 0:
 956                mode |= S_IFREG;
 957                /* for mode = 0 case */
 958                /* fall through */
 959        case S_IFREG:
 960        case S_IFCHR:
 961        case S_IFBLK:
 962        case S_IFIFO:
 963        case S_IFSOCK:
 964                err = ll_new_node(dir, dchild, NULL, mode,
 965                                  old_encode_dev(rdev),
 966                                  LUSTRE_OPC_MKNOD);
 967                break;
 968        case S_IFDIR:
 969                err = -EPERM;
 970                break;
 971        default:
 972                err = -EINVAL;
 973        }
 974
 975        if (!err)
 976                ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKNOD, 1);
 977
 978        return err;
 979}
 980
 981/*
 982 * Plain create. Intent create is handled in atomic_open.
 983 */
 984static int ll_create_nd(struct inode *dir, struct dentry *dentry,
 985                        umode_t mode, bool want_excl)
 986{
 987        int rc;
 988
 989        CDEBUG(D_VFSTRACE,
 990               "VFS Op:name=%pd, dir=" DFID "(%p), flags=%u, excl=%d\n",
 991               dentry, PFID(ll_inode2fid(dir)), dir, mode, want_excl);
 992
 993        rc = ll_mknod(dir, dentry, mode, 0);
 994
 995        ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
 996
 997        CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, unhashed %d\n",
 998               dentry, d_unhashed(dentry));
 999
1000        return rc;
1001}
1002
1003static int ll_unlink(struct inode *dir, struct dentry *dchild)
1004{
1005        struct ptlrpc_request *request = NULL;
1006        struct md_op_data *op_data;
1007        int rc;
1008
1009        CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p)\n",
1010               dchild, dir->i_ino, dir->i_generation, dir);
1011
1012        op_data = ll_prep_md_op_data(NULL, dir, NULL,
1013                                     dchild->d_name.name,
1014                                     dchild->d_name.len,
1015                                     0, LUSTRE_OPC_ANY, NULL);
1016        if (IS_ERR(op_data))
1017                return PTR_ERR(op_data);
1018
1019        if (dchild->d_inode)
1020                op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1021
1022        op_data->op_fid2 = op_data->op_fid3;
1023        rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1024        ll_finish_md_op_data(op_data);
1025        if (rc)
1026                goto out;
1027
1028        ll_update_times(request, dir);
1029        ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_UNLINK, 1);
1030
1031 out:
1032        ptlrpc_req_finished(request);
1033        return rc;
1034}
1035
1036static int ll_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1037{
1038        int err;
1039
1040        CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir" DFID "(%p)\n",
1041               dentry, PFID(ll_inode2fid(dir)), dir);
1042
1043        if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1044                mode &= ~current_umask();
1045        mode = (mode & (0777 | S_ISVTX)) | S_IFDIR;
1046
1047        err = ll_new_node(dir, dentry, NULL, mode, 0, LUSTRE_OPC_MKDIR);
1048        if (!err)
1049                ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKDIR, 1);
1050
1051        return err;
1052}
1053
1054static int ll_rmdir(struct inode *dir, struct dentry *dchild)
1055{
1056        struct ptlrpc_request *request = NULL;
1057        struct md_op_data *op_data;
1058        int rc;
1059
1060        CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir=" DFID "(%p)\n",
1061               dchild, PFID(ll_inode2fid(dir)), dir);
1062
1063        op_data = ll_prep_md_op_data(NULL, dir, NULL,
1064                                     dchild->d_name.name,
1065                                     dchild->d_name.len,
1066                                     S_IFDIR, LUSTRE_OPC_ANY, NULL);
1067        if (IS_ERR(op_data))
1068                return PTR_ERR(op_data);
1069
1070        if (dchild->d_inode)
1071                op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1072
1073        op_data->op_fid2 = op_data->op_fid3;
1074        rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1075        ll_finish_md_op_data(op_data);
1076        if (rc == 0) {
1077                ll_update_times(request, dir);
1078                ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1079        }
1080
1081        ptlrpc_req_finished(request);
1082        return rc;
1083}
1084
1085static int ll_symlink(struct inode *dir, struct dentry *dentry,
1086                      const char *oldname)
1087{
1088        int err;
1089
1090        CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir=" DFID "(%p),target=%.*s\n",
1091               dentry, PFID(ll_inode2fid(dir)), dir, 3000, oldname);
1092
1093        err = ll_new_node(dir, dentry, oldname, S_IFLNK | 0777,
1094                          0, LUSTRE_OPC_SYMLINK);
1095
1096        if (!err)
1097                ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_SYMLINK, 1);
1098
1099        return err;
1100}
1101
1102static int ll_link(struct dentry *old_dentry, struct inode *dir,
1103                   struct dentry *new_dentry)
1104{
1105        struct inode *src = d_inode(old_dentry);
1106        struct ll_sb_info *sbi = ll_i2sbi(dir);
1107        struct ptlrpc_request *request = NULL;
1108        struct md_op_data *op_data;
1109        int err;
1110
1111        CDEBUG(D_VFSTRACE,
1112               "VFS Op: inode=" DFID "(%p), dir=" DFID "(%p), target=%pd\n",
1113               PFID(ll_inode2fid(src)), src, PFID(ll_inode2fid(dir)), dir,
1114               new_dentry);
1115
1116        op_data = ll_prep_md_op_data(NULL, src, dir, new_dentry->d_name.name,
1117                                     new_dentry->d_name.len,
1118                                     0, LUSTRE_OPC_ANY, NULL);
1119        if (IS_ERR(op_data))
1120                return PTR_ERR(op_data);
1121
1122        err = md_link(sbi->ll_md_exp, op_data, &request);
1123        ll_finish_md_op_data(op_data);
1124        if (err)
1125                goto out;
1126
1127        ll_update_times(request, dir);
1128        ll_stats_ops_tally(sbi, LPROC_LL_LINK, 1);
1129out:
1130        ptlrpc_req_finished(request);
1131        return err;
1132}
1133
1134static int ll_rename(struct inode *src, struct dentry *src_dchild,
1135                     struct inode *tgt, struct dentry *tgt_dchild,
1136                     unsigned int flags)
1137{
1138        struct ptlrpc_request *request = NULL;
1139        struct ll_sb_info *sbi = ll_i2sbi(src);
1140        struct md_op_data *op_data;
1141        int err;
1142
1143        if (flags)
1144                return -EINVAL;
1145
1146        CDEBUG(D_VFSTRACE,
1147               "VFS Op:oldname=%pd, src_dir=" DFID "(%p), newname=%pd, tgt_dir=" DFID "(%p)\n",
1148               src_dchild, PFID(ll_inode2fid(src)), src,
1149               tgt_dchild, PFID(ll_inode2fid(tgt)), tgt);
1150
1151        op_data = ll_prep_md_op_data(NULL, src, tgt, NULL, 0, 0,
1152                                     LUSTRE_OPC_ANY, NULL);
1153        if (IS_ERR(op_data))
1154                return PTR_ERR(op_data);
1155
1156        if (src_dchild->d_inode)
1157                op_data->op_fid3 = *ll_inode2fid(src_dchild->d_inode);
1158        if (tgt_dchild->d_inode)
1159                op_data->op_fid4 = *ll_inode2fid(tgt_dchild->d_inode);
1160
1161        err = md_rename(sbi->ll_md_exp, op_data,
1162                        src_dchild->d_name.name,
1163                        src_dchild->d_name.len,
1164                        tgt_dchild->d_name.name,
1165                        tgt_dchild->d_name.len, &request);
1166        ll_finish_md_op_data(op_data);
1167        if (!err) {
1168                ll_update_times(request, src);
1169                ll_update_times(request, tgt);
1170                ll_stats_ops_tally(sbi, LPROC_LL_RENAME, 1);
1171        }
1172
1173        ptlrpc_req_finished(request);
1174        if (!err)
1175                d_move(src_dchild, tgt_dchild);
1176        return err;
1177}
1178
1179const struct inode_operations ll_dir_inode_operations = {
1180        .mknod        = ll_mknod,
1181        .atomic_open        = ll_atomic_open,
1182        .lookup      = ll_lookup_nd,
1183        .create      = ll_create_nd,
1184        /* We need all these non-raw things for NFSD, to not patch it. */
1185        .unlink      = ll_unlink,
1186        .mkdir        = ll_mkdir,
1187        .rmdir        = ll_rmdir,
1188        .symlink            = ll_symlink,
1189        .link          = ll_link,
1190        .rename         = ll_rename,
1191        .setattr            = ll_setattr,
1192        .getattr            = ll_getattr,
1193        .permission      = ll_inode_permission,
1194        .listxattr        = ll_listxattr,
1195        .get_acl            = ll_get_acl,
1196};
1197
1198const struct inode_operations ll_special_inode_operations = {
1199        .setattr        = ll_setattr,
1200        .getattr        = ll_getattr,
1201        .permission     = ll_inode_permission,
1202        .listxattr      = ll_listxattr,
1203        .get_acl            = ll_get_acl,
1204};
1205