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