Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/cifs/inode.c |
| 3 | * |
Steve French | f19159d | 2010-04-21 04:12:10 +0000 | [diff] [blame] | 4 | * Copyright (C) International Business Machines Corp., 2002,2010 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Author(s): Steve French (sfrench@us.ibm.com) |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU Lesser General Public License as published |
| 9 | * by the Free Software Foundation; either version 2.1 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 15 | * the GNU Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public License |
| 18 | * along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/stat.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/pagemap.h> |
Jeff Layton | 4f73c7d | 2014-04-30 09:31:47 -0400 | [diff] [blame] | 25 | #include <linux/freezer.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 26 | #include <linux/sched/signal.h> |
Ingo Molnar | 5dd43ce | 2017-06-20 12:19:09 +0200 | [diff] [blame] | 27 | #include <linux/wait_bit.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <asm/div64.h> |
| 30 | #include "cifsfs.h" |
| 31 | #include "cifspdu.h" |
| 32 | #include "cifsglob.h" |
| 33 | #include "cifsproto.h" |
| 34 | #include "cifs_debug.h" |
| 35 | #include "cifs_fs_sb.h" |
Steve French | 2baa268 | 2014-09-27 02:19:01 -0500 | [diff] [blame] | 36 | #include "cifs_unicode.h" |
Suresh Jayaraman | 9451a9a | 2010-07-05 18:12:45 +0530 | [diff] [blame] | 37 | #include "fscache.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Christoph Hellwig | 70eff55 | 2008-02-15 20:55:05 +0000 | [diff] [blame] | 39 | |
David Howells | 01c64fe | 2011-01-14 18:45:47 +0000 | [diff] [blame] | 40 | static void cifs_set_ops(struct inode *inode) |
Christoph Hellwig | 70eff55 | 2008-02-15 20:55:05 +0000 | [diff] [blame] | 41 | { |
| 42 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 43 | |
| 44 | switch (inode->i_mode & S_IFMT) { |
| 45 | case S_IFREG: |
| 46 | inode->i_op = &cifs_file_inode_ops; |
| 47 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) { |
| 48 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) |
| 49 | inode->i_fop = &cifs_file_direct_nobrl_ops; |
| 50 | else |
| 51 | inode->i_fop = &cifs_file_direct_ops; |
Pavel Shilovsky | 8be7e6b | 2010-12-12 13:11:13 +0300 | [diff] [blame] | 52 | } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) { |
| 53 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) |
| 54 | inode->i_fop = &cifs_file_strict_nobrl_ops; |
| 55 | else |
| 56 | inode->i_fop = &cifs_file_strict_ops; |
Christoph Hellwig | 70eff55 | 2008-02-15 20:55:05 +0000 | [diff] [blame] | 57 | } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) |
| 58 | inode->i_fop = &cifs_file_nobrl_ops; |
| 59 | else { /* not direct, send byte range locks */ |
| 60 | inode->i_fop = &cifs_file_ops; |
| 61 | } |
| 62 | |
Christoph Hellwig | 70eff55 | 2008-02-15 20:55:05 +0000 | [diff] [blame] | 63 | /* check if server can support readpages */ |
Jeff Layton | 0d424ad | 2010-09-20 16:01:35 -0700 | [diff] [blame] | 64 | if (cifs_sb_master_tcon(cifs_sb)->ses->server->maxBuf < |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 65 | PAGE_SIZE + MAX_CIFS_HDR_SIZE) |
Christoph Hellwig | 70eff55 | 2008-02-15 20:55:05 +0000 | [diff] [blame] | 66 | inode->i_data.a_ops = &cifs_addr_ops_smallbuf; |
| 67 | else |
| 68 | inode->i_data.a_ops = &cifs_addr_ops; |
| 69 | break; |
| 70 | case S_IFDIR: |
Steve French | bc5b6e2 | 2008-03-11 21:07:48 +0000 | [diff] [blame] | 71 | #ifdef CONFIG_CIFS_DFS_UPCALL |
David Howells | 01c64fe | 2011-01-14 18:45:47 +0000 | [diff] [blame] | 72 | if (IS_AUTOMOUNT(inode)) { |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 73 | inode->i_op = &cifs_dfs_referral_inode_operations; |
| 74 | } else { |
Steve French | bc5b6e2 | 2008-03-11 21:07:48 +0000 | [diff] [blame] | 75 | #else /* NO DFS support, treat as a directory */ |
| 76 | { |
| 77 | #endif |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 78 | inode->i_op = &cifs_dir_inode_ops; |
| 79 | inode->i_fop = &cifs_dir_ops; |
| 80 | } |
Christoph Hellwig | 70eff55 | 2008-02-15 20:55:05 +0000 | [diff] [blame] | 81 | break; |
| 82 | case S_IFLNK: |
| 83 | inode->i_op = &cifs_symlink_inode_ops; |
| 84 | break; |
| 85 | default: |
| 86 | init_special_inode(inode, inode->i_mode, inode->i_rdev); |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 91 | /* check inode attributes against fattr. If they don't match, tag the |
| 92 | * inode for cache invalidation |
| 93 | */ |
| 94 | static void |
| 95 | cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr) |
| 96 | { |
| 97 | struct cifsInodeInfo *cifs_i = CIFS_I(inode); |
| 98 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 99 | cifs_dbg(FYI, "%s: revalidating inode %llu\n", |
| 100 | __func__, cifs_i->uniqueid); |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 101 | |
| 102 | if (inode->i_state & I_NEW) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 103 | cifs_dbg(FYI, "%s: inode %llu is new\n", |
| 104 | __func__, cifs_i->uniqueid); |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 105 | return; |
| 106 | } |
| 107 | |
| 108 | /* don't bother with revalidation if we have an oplock */ |
Pavel Shilovsky | 18cceb6 | 2013-09-05 13:01:06 +0400 | [diff] [blame] | 109 | if (CIFS_CACHE_READ(cifs_i)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 110 | cifs_dbg(FYI, "%s: inode %llu is oplocked\n", |
| 111 | __func__, cifs_i->uniqueid); |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 112 | return; |
| 113 | } |
| 114 | |
| 115 | /* revalidate if mtime or size have changed */ |
Arnd Bergmann | 9539020 | 2018-06-19 17:27:58 +0200 | [diff] [blame] | 116 | if (timespec64_equal(&inode->i_mtime, &fattr->cf_mtime) && |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 117 | cifs_i->server_eof == fattr->cf_eof) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 118 | cifs_dbg(FYI, "%s: inode %llu is unchanged\n", |
| 119 | __func__, cifs_i->uniqueid); |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 120 | return; |
| 121 | } |
| 122 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 123 | cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n", |
| 124 | __func__, cifs_i->uniqueid); |
Jeff Layton | aff8d5c | 2014-04-30 09:31:45 -0400 | [diff] [blame] | 125 | set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags); |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 126 | } |
| 127 | |
Jim McDonough | 74d290d | 2013-09-21 10:36:10 -0500 | [diff] [blame] | 128 | /* |
| 129 | * copy nlink to the inode, unless it wasn't provided. Provide |
| 130 | * sane values if we don't have an existing one and none was provided |
| 131 | */ |
| 132 | static void |
| 133 | cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr) |
| 134 | { |
| 135 | /* |
| 136 | * if we're in a situation where we can't trust what we |
| 137 | * got from the server (readdir, some non-unix cases) |
| 138 | * fake reasonable values |
| 139 | */ |
| 140 | if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) { |
| 141 | /* only provide fake values on a new inode */ |
| 142 | if (inode->i_state & I_NEW) { |
| 143 | if (fattr->cf_cifsattrs & ATTR_DIRECTORY) |
| 144 | set_nlink(inode, 2); |
| 145 | else |
| 146 | set_nlink(inode, 1); |
| 147 | } |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | /* we trust the server, so update it */ |
| 152 | set_nlink(inode, fattr->cf_nlink); |
| 153 | } |
| 154 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 155 | /* populate an inode with info from a cifs_fattr struct */ |
| 156 | void |
| 157 | cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr) |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 158 | { |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 159 | struct cifsInodeInfo *cifs_i = CIFS_I(inode); |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 160 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 161 | |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 162 | cifs_revalidate_cache(inode, fattr); |
| 163 | |
Steve French | b7ca692 | 2012-08-03 08:43:01 -0500 | [diff] [blame] | 164 | spin_lock(&inode->i_lock); |
Steve French | 9b9c5be | 2018-09-22 12:07:06 -0500 | [diff] [blame] | 165 | /* we do not want atime to be less than mtime, it broke some apps */ |
| 166 | if (timespec64_compare(&fattr->cf_atime, &fattr->cf_mtime)) |
| 167 | inode->i_atime = fattr->cf_mtime; |
| 168 | else |
| 169 | inode->i_atime = fattr->cf_atime; |
Arnd Bergmann | 9539020 | 2018-06-19 17:27:58 +0200 | [diff] [blame] | 170 | inode->i_mtime = fattr->cf_mtime; |
| 171 | inode->i_ctime = fattr->cf_ctime; |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 172 | inode->i_rdev = fattr->cf_rdev; |
Jim McDonough | 74d290d | 2013-09-21 10:36:10 -0500 | [diff] [blame] | 173 | cifs_nlink_fattr_to_inode(inode, fattr); |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 174 | inode->i_uid = fattr->cf_uid; |
| 175 | inode->i_gid = fattr->cf_gid; |
| 176 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 177 | /* if dynperm is set, don't clobber existing mode */ |
| 178 | if (inode->i_state & I_NEW || |
| 179 | !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) |
| 180 | inode->i_mode = fattr->cf_mode; |
| 181 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 182 | cifs_i->cifsAttrs = fattr->cf_cifsattrs; |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 183 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 184 | if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL) |
| 185 | cifs_i->time = 0; |
| 186 | else |
| 187 | cifs_i->time = jiffies; |
| 188 | |
Jeff Layton | aff8d5c | 2014-04-30 09:31:45 -0400 | [diff] [blame] | 189 | if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING) |
| 190 | set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags); |
| 191 | else |
| 192 | clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags); |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 193 | |
Jeff Layton | 835a36c | 2010-02-10 16:21:33 -0500 | [diff] [blame] | 194 | cifs_i->server_eof = fattr->cf_eof; |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 195 | /* |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 196 | * Can't safely change the file size here if the client is writing to |
| 197 | * it due to potential races. |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 198 | */ |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 199 | if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) { |
| 200 | i_size_write(inode, fattr->cf_eof); |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 201 | |
| 202 | /* |
| 203 | * i_blocks is not related to (i_size / i_blksize), |
| 204 | * but instead 512 byte (2**9) size is required for |
| 205 | * calculating num blocks. |
| 206 | */ |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 207 | inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9; |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 208 | } |
| 209 | spin_unlock(&inode->i_lock); |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 210 | |
David Howells | 01c64fe | 2011-01-14 18:45:47 +0000 | [diff] [blame] | 211 | if (fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL) |
| 212 | inode->i_flags |= S_AUTOMOUNT; |
Jeff Layton | c2b93e0 | 2013-05-07 11:28:31 -0400 | [diff] [blame] | 213 | if (inode->i_state & I_NEW) |
| 214 | cifs_set_ops(inode); |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Jeff Layton | 4065c80 | 2010-05-17 07:18:58 -0400 | [diff] [blame] | 217 | void |
| 218 | cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr) |
| 219 | { |
| 220 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
| 221 | |
| 222 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) |
| 223 | return; |
| 224 | |
| 225 | fattr->cf_uniqueid = iunique(sb, ROOT_I); |
| 226 | } |
| 227 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 228 | /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */ |
| 229 | void |
| 230 | cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info, |
| 231 | struct cifs_sb_info *cifs_sb) |
| 232 | { |
| 233 | memset(fattr, 0, sizeof(*fattr)); |
| 234 | fattr->cf_uniqueid = le64_to_cpu(info->UniqueId); |
| 235 | fattr->cf_bytes = le64_to_cpu(info->NumOfBytes); |
| 236 | fattr->cf_eof = le64_to_cpu(info->EndOfFile); |
| 237 | |
| 238 | fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime); |
| 239 | fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime); |
| 240 | fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange); |
Steve French | 6e70e26 | 2017-09-21 21:32:29 -0500 | [diff] [blame] | 241 | /* old POSIX extensions don't get create time */ |
| 242 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 243 | fattr->cf_mode = le64_to_cpu(info->Permissions); |
| 244 | |
| 245 | /* |
| 246 | * Since we set the inode type below we need to mask off |
| 247 | * to avoid strange results if bits set above. |
| 248 | */ |
| 249 | fattr->cf_mode &= ~S_IFMT; |
| 250 | switch (le32_to_cpu(info->Type)) { |
| 251 | case UNIX_FILE: |
| 252 | fattr->cf_mode |= S_IFREG; |
| 253 | fattr->cf_dtype = DT_REG; |
| 254 | break; |
| 255 | case UNIX_SYMLINK: |
| 256 | fattr->cf_mode |= S_IFLNK; |
| 257 | fattr->cf_dtype = DT_LNK; |
| 258 | break; |
| 259 | case UNIX_DIR: |
| 260 | fattr->cf_mode |= S_IFDIR; |
| 261 | fattr->cf_dtype = DT_DIR; |
| 262 | break; |
| 263 | case UNIX_CHARDEV: |
| 264 | fattr->cf_mode |= S_IFCHR; |
| 265 | fattr->cf_dtype = DT_CHR; |
| 266 | fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor), |
| 267 | le64_to_cpu(info->DevMinor) & MINORMASK); |
| 268 | break; |
| 269 | case UNIX_BLOCKDEV: |
| 270 | fattr->cf_mode |= S_IFBLK; |
| 271 | fattr->cf_dtype = DT_BLK; |
| 272 | fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor), |
| 273 | le64_to_cpu(info->DevMinor) & MINORMASK); |
| 274 | break; |
| 275 | case UNIX_FIFO: |
| 276 | fattr->cf_mode |= S_IFIFO; |
| 277 | fattr->cf_dtype = DT_FIFO; |
| 278 | break; |
| 279 | case UNIX_SOCKET: |
| 280 | fattr->cf_mode |= S_IFSOCK; |
| 281 | fattr->cf_dtype = DT_SOCK; |
| 282 | break; |
| 283 | default: |
| 284 | /* safest to call it a file if we do not know */ |
| 285 | fattr->cf_mode |= S_IFREG; |
| 286 | fattr->cf_dtype = DT_REG; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 287 | cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type)); |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 288 | break; |
| 289 | } |
| 290 | |
Eric W. Biederman | 46bbc25 | 2013-02-05 23:55:44 -0800 | [diff] [blame] | 291 | fattr->cf_uid = cifs_sb->mnt_uid; |
| 292 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) { |
| 293 | u64 id = le64_to_cpu(info->Uid); |
Eric W. Biederman | 4a2c8cf | 2013-02-06 01:53:25 -0800 | [diff] [blame] | 294 | if (id < ((uid_t)-1)) { |
| 295 | kuid_t uid = make_kuid(&init_user_ns, id); |
| 296 | if (uid_valid(uid)) |
| 297 | fattr->cf_uid = uid; |
| 298 | } |
Eric W. Biederman | 46bbc25 | 2013-02-05 23:55:44 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | fattr->cf_gid = cifs_sb->mnt_gid; |
| 302 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) { |
| 303 | u64 id = le64_to_cpu(info->Gid); |
Eric W. Biederman | 4a2c8cf | 2013-02-06 01:53:25 -0800 | [diff] [blame] | 304 | if (id < ((gid_t)-1)) { |
| 305 | kgid_t gid = make_kgid(&init_user_ns, id); |
| 306 | if (gid_valid(gid)) |
| 307 | fattr->cf_gid = gid; |
| 308 | } |
Eric W. Biederman | 46bbc25 | 2013-02-05 23:55:44 -0800 | [diff] [blame] | 309 | } |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 310 | |
| 311 | fattr->cf_nlink = le64_to_cpu(info->Nlinks); |
| 312 | } |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 313 | |
| 314 | /* |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 315 | * Fill a cifs_fattr struct with fake inode info. |
| 316 | * |
| 317 | * Needed to setup cifs_fattr data for the directory which is the |
| 318 | * junction to the new submount (ie to setup the fake directory |
| 319 | * which represents a DFS referral). |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 320 | */ |
Steve French | f1230c9 | 2009-07-22 23:13:01 +0000 | [diff] [blame] | 321 | static void |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 322 | cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb) |
Steve French | 0e4bbde | 2008-05-20 19:50:46 +0000 | [diff] [blame] | 323 | { |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 324 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
Steve French | 0e4bbde | 2008-05-20 19:50:46 +0000 | [diff] [blame] | 325 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 326 | cifs_dbg(FYI, "creating fake fattr for DFS referral\n"); |
Steve French | 0e4bbde | 2008-05-20 19:50:46 +0000 | [diff] [blame] | 327 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 328 | memset(fattr, 0, sizeof(*fattr)); |
| 329 | fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU; |
| 330 | fattr->cf_uid = cifs_sb->mnt_uid; |
| 331 | fattr->cf_gid = cifs_sb->mnt_gid; |
Arnd Bergmann | 9539020 | 2018-06-19 17:27:58 +0200 | [diff] [blame] | 332 | ktime_get_real_ts64(&fattr->cf_mtime); |
| 333 | fattr->cf_mtime = timespec64_trunc(fattr->cf_mtime, sb->s_time_gran); |
Deepa Dinamani | e37fea5 | 2017-05-08 15:59:16 -0700 | [diff] [blame] | 334 | fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime; |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 335 | fattr->cf_nlink = 2; |
Steve French | 97aa495 | 2018-11-15 00:33:05 -0600 | [diff] [blame] | 336 | fattr->cf_flags = CIFS_FATTR_DFS_REFERRAL; |
Steve French | 0e4bbde | 2008-05-20 19:50:46 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Pavel Shilovsky | 4ad6504 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 339 | static int |
| 340 | cifs_get_file_info_unix(struct file *filp) |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 341 | { |
| 342 | int rc; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 343 | unsigned int xid; |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 344 | FILE_UNIX_BASIC_INFO find_data; |
| 345 | struct cifs_fattr fattr; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 346 | struct inode *inode = file_inode(filp); |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 347 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
Joe Perches | c21dfb6 | 2010-07-12 13:50:14 -0700 | [diff] [blame] | 348 | struct cifsFileInfo *cfile = filp->private_data; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 349 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 350 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 351 | xid = get_xid(); |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 352 | rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data); |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 353 | if (!rc) { |
| 354 | cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb); |
| 355 | } else if (rc == -EREMOTE) { |
| 356 | cifs_create_dfs_fattr(&fattr, inode->i_sb); |
| 357 | rc = 0; |
| 358 | } |
| 359 | |
| 360 | cifs_fattr_to_inode(inode, &fattr); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 361 | free_xid(xid); |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 362 | return rc; |
| 363 | } |
| 364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | int cifs_get_inode_info_unix(struct inode **pinode, |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 366 | const unsigned char *full_path, |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 367 | struct super_block *sb, unsigned int xid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | { |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 369 | int rc; |
Steve French | 0e4bbde | 2008-05-20 19:50:46 +0000 | [diff] [blame] | 370 | FILE_UNIX_BASIC_INFO find_data; |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 371 | struct cifs_fattr fattr; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 372 | struct cifs_tcon *tcon; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 373 | struct tcon_link *tlink; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 376 | cifs_dbg(FYI, "Getting info on %s\n", full_path); |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 377 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 378 | tlink = cifs_sb_tlink(cifs_sb); |
| 379 | if (IS_ERR(tlink)) |
| 380 | return PTR_ERR(tlink); |
| 381 | tcon = tlink_tcon(tlink); |
| 382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | /* could have done a find first instead but this returns more info */ |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 384 | rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data, |
Nakajima Akira | bc8ebdc4 | 2015-02-13 15:35:58 +0900 | [diff] [blame] | 385 | cifs_sb->local_nls, cifs_remap(cifs_sb)); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 386 | cifs_put_tlink(tlink); |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 387 | |
| 388 | if (!rc) { |
| 389 | cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb); |
| 390 | } else if (rc == -EREMOTE) { |
| 391 | cifs_create_dfs_fattr(&fattr, sb); |
Jeff Layton | e911d0c | 2008-07-12 13:47:59 -0700 | [diff] [blame] | 392 | rc = 0; |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 393 | } else { |
| 394 | return rc; |
Steve French | 0e4bbde | 2008-05-20 19:50:46 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Stefan Metzmacher | 1b12b9c | 2010-08-05 21:19:56 +0200 | [diff] [blame] | 397 | /* check for Minshall+French symlinks */ |
| 398 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) { |
Sachin Prabhu | cb084b1 | 2013-11-25 17:09:50 +0000 | [diff] [blame] | 399 | int tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr, |
| 400 | full_path); |
Stefan Metzmacher | 1b12b9c | 2010-08-05 21:19:56 +0200 | [diff] [blame] | 401 | if (tmprc) |
Sachin Prabhu | cb084b1 | 2013-11-25 17:09:50 +0000 | [diff] [blame] | 402 | cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc); |
Stefan Metzmacher | 1b12b9c | 2010-08-05 21:19:56 +0200 | [diff] [blame] | 403 | } |
| 404 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 405 | if (*pinode == NULL) { |
| 406 | /* get new inode */ |
Jeff Layton | 4065c80 | 2010-05-17 07:18:58 -0400 | [diff] [blame] | 407 | cifs_fill_uniqueid(sb, &fattr); |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 408 | *pinode = cifs_iget(sb, &fattr); |
| 409 | if (!*pinode) |
| 410 | rc = -ENOMEM; |
| 411 | } else { |
| 412 | /* we already have inode, update it */ |
Nakajima Akira | 7196ac1 | 2015-04-22 15:24:44 +0900 | [diff] [blame] | 413 | |
| 414 | /* if uniqueid is different, return error */ |
| 415 | if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM && |
| 416 | CIFS_I(*pinode)->uniqueid != fattr.cf_uniqueid)) { |
| 417 | rc = -ESTALE; |
| 418 | goto cgiiu_exit; |
| 419 | } |
| 420 | |
| 421 | /* if filetype is different, return error */ |
| 422 | if (unlikely(((*pinode)->i_mode & S_IFMT) != |
| 423 | (fattr.cf_mode & S_IFMT))) { |
| 424 | rc = -ESTALE; |
| 425 | goto cgiiu_exit; |
| 426 | } |
| 427 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 428 | cifs_fattr_to_inode(*pinode, &fattr); |
| 429 | } |
Steve French | 0e4bbde | 2008-05-20 19:50:46 +0000 | [diff] [blame] | 430 | |
Nakajima Akira | 7196ac1 | 2015-04-22 15:24:44 +0900 | [diff] [blame] | 431 | cgiiu_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | return rc; |
| 433 | } |
| 434 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 435 | static int |
Pavel Shilovsky | 0360d60 | 2014-01-16 15:53:35 +0400 | [diff] [blame] | 436 | cifs_sfu_type(struct cifs_fattr *fattr, const char *path, |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 437 | struct cifs_sb_info *cifs_sb, unsigned int xid) |
Steve French | d6e2f2a4 | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 438 | { |
| 439 | int rc; |
Steve French | db8b631 | 2014-09-22 05:13:55 -0500 | [diff] [blame] | 440 | __u32 oplock; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 441 | struct tcon_link *tlink; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 442 | struct cifs_tcon *tcon; |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 443 | struct cifs_fid fid; |
| 444 | struct cifs_open_parms oparms; |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 445 | struct cifs_io_parms io_parms; |
Steve French | 86c96b4 | 2005-11-18 20:25:31 -0800 | [diff] [blame] | 446 | char buf[24]; |
Steve French | d6e2f2a4 | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 447 | unsigned int bytes_read; |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 448 | char *pbuf; |
Pavel Shilovsky | 0360d60 | 2014-01-16 15:53:35 +0400 | [diff] [blame] | 449 | int buf_type = CIFS_NO_BUFFER; |
Steve French | d6e2f2a4 | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 450 | |
| 451 | pbuf = buf; |
| 452 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 453 | fattr->cf_mode &= ~S_IFMT; |
| 454 | |
| 455 | if (fattr->cf_eof == 0) { |
| 456 | fattr->cf_mode |= S_IFIFO; |
| 457 | fattr->cf_dtype = DT_FIFO; |
Steve French | d6e2f2a4 | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 458 | return 0; |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 459 | } else if (fattr->cf_eof < 8) { |
| 460 | fattr->cf_mode |= S_IFREG; |
| 461 | fattr->cf_dtype = DT_REG; |
Steve French | d6e2f2a4 | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 462 | return -EINVAL; /* EOPNOTSUPP? */ |
| 463 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 464 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 465 | tlink = cifs_sb_tlink(cifs_sb); |
| 466 | if (IS_ERR(tlink)) |
| 467 | return PTR_ERR(tlink); |
| 468 | tcon = tlink_tcon(tlink); |
| 469 | |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 470 | oparms.tcon = tcon; |
| 471 | oparms.cifs_sb = cifs_sb; |
| 472 | oparms.desired_access = GENERIC_READ; |
| 473 | oparms.create_options = CREATE_NOT_DIR; |
Steve French | 5e19697 | 2018-08-27 17:04:13 -0500 | [diff] [blame] | 474 | if (backup_cred(cifs_sb)) |
| 475 | oparms.create_options |= CREATE_OPEN_BACKUP_INTENT; |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 476 | oparms.disposition = FILE_OPEN; |
| 477 | oparms.path = path; |
| 478 | oparms.fid = &fid; |
| 479 | oparms.reconnect = false; |
| 480 | |
Steve French | db8b631 | 2014-09-22 05:13:55 -0500 | [diff] [blame] | 481 | if (tcon->ses->server->oplocks) |
| 482 | oplock = REQ_OPLOCK; |
| 483 | else |
| 484 | oplock = 0; |
| 485 | rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL); |
Pavel Shilovsky | 0360d60 | 2014-01-16 15:53:35 +0400 | [diff] [blame] | 486 | if (rc) { |
Steve French | db8b631 | 2014-09-22 05:13:55 -0500 | [diff] [blame] | 487 | cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc); |
Pavel Shilovsky | 0360d60 | 2014-01-16 15:53:35 +0400 | [diff] [blame] | 488 | cifs_put_tlink(tlink); |
| 489 | return rc; |
Steve French | d6e2f2a4 | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 490 | } |
Pavel Shilovsky | 0360d60 | 2014-01-16 15:53:35 +0400 | [diff] [blame] | 491 | |
| 492 | /* Read header */ |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 493 | io_parms.netfid = fid.netfid; |
Pavel Shilovsky | 0360d60 | 2014-01-16 15:53:35 +0400 | [diff] [blame] | 494 | io_parms.pid = current->tgid; |
| 495 | io_parms.tcon = tcon; |
| 496 | io_parms.offset = 0; |
| 497 | io_parms.length = 24; |
| 498 | |
Steve French | db8b631 | 2014-09-22 05:13:55 -0500 | [diff] [blame] | 499 | rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms, |
| 500 | &bytes_read, &pbuf, &buf_type); |
Pavel Shilovsky | 0360d60 | 2014-01-16 15:53:35 +0400 | [diff] [blame] | 501 | if ((rc == 0) && (bytes_read >= 8)) { |
| 502 | if (memcmp("IntxBLK", pbuf, 8) == 0) { |
| 503 | cifs_dbg(FYI, "Block device\n"); |
| 504 | fattr->cf_mode |= S_IFBLK; |
| 505 | fattr->cf_dtype = DT_BLK; |
| 506 | if (bytes_read == 24) { |
| 507 | /* we have enough to decode dev num */ |
| 508 | __u64 mjr; /* major */ |
| 509 | __u64 mnr; /* minor */ |
| 510 | mjr = le64_to_cpu(*(__le64 *)(pbuf+8)); |
| 511 | mnr = le64_to_cpu(*(__le64 *)(pbuf+16)); |
| 512 | fattr->cf_rdev = MKDEV(mjr, mnr); |
| 513 | } |
| 514 | } else if (memcmp("IntxCHR", pbuf, 8) == 0) { |
| 515 | cifs_dbg(FYI, "Char device\n"); |
| 516 | fattr->cf_mode |= S_IFCHR; |
| 517 | fattr->cf_dtype = DT_CHR; |
| 518 | if (bytes_read == 24) { |
| 519 | /* we have enough to decode dev num */ |
| 520 | __u64 mjr; /* major */ |
| 521 | __u64 mnr; /* minor */ |
| 522 | mjr = le64_to_cpu(*(__le64 *)(pbuf+8)); |
| 523 | mnr = le64_to_cpu(*(__le64 *)(pbuf+16)); |
| 524 | fattr->cf_rdev = MKDEV(mjr, mnr); |
| 525 | } |
| 526 | } else if (memcmp("IntxLNK", pbuf, 7) == 0) { |
| 527 | cifs_dbg(FYI, "Symlink\n"); |
| 528 | fattr->cf_mode |= S_IFLNK; |
| 529 | fattr->cf_dtype = DT_LNK; |
| 530 | } else { |
| 531 | fattr->cf_mode |= S_IFREG; /* file? */ |
| 532 | fattr->cf_dtype = DT_REG; |
| 533 | rc = -EOPNOTSUPP; |
| 534 | } |
| 535 | } else { |
| 536 | fattr->cf_mode |= S_IFREG; /* then it is a file */ |
| 537 | fattr->cf_dtype = DT_REG; |
| 538 | rc = -EOPNOTSUPP; /* or some unknown SFU type */ |
| 539 | } |
Steve French | db8b631 | 2014-09-22 05:13:55 -0500 | [diff] [blame] | 540 | |
| 541 | tcon->ses->server->ops->close(xid, tcon, &fid); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 542 | cifs_put_tlink(tlink); |
Steve French | d6e2f2a4 | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 543 | return rc; |
Steve French | d6e2f2a4 | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 544 | } |
| 545 | |
Steve French | 9e294f1 | 2005-11-17 16:59:21 -0800 | [diff] [blame] | 546 | #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */ |
| 547 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 548 | /* |
| 549 | * Fetch mode bits as provided by SFU. |
| 550 | * |
| 551 | * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ? |
| 552 | */ |
| 553 | static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path, |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 554 | struct cifs_sb_info *cifs_sb, unsigned int xid) |
Steve French | 9e294f1 | 2005-11-17 16:59:21 -0800 | [diff] [blame] | 555 | { |
Steve French | 3020a1f | 2005-11-18 11:31:10 -0800 | [diff] [blame] | 556 | #ifdef CONFIG_CIFS_XATTR |
Steve French | 9e294f1 | 2005-11-17 16:59:21 -0800 | [diff] [blame] | 557 | ssize_t rc; |
| 558 | char ea_value[4]; |
| 559 | __u32 mode; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 560 | struct tcon_link *tlink; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 561 | struct cifs_tcon *tcon; |
Steve French | 9e294f1 | 2005-11-17 16:59:21 -0800 | [diff] [blame] | 562 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 563 | tlink = cifs_sb_tlink(cifs_sb); |
| 564 | if (IS_ERR(tlink)) |
| 565 | return PTR_ERR(tlink); |
| 566 | tcon = tlink_tcon(tlink); |
| 567 | |
Steve French | d979f3b | 2014-02-01 23:27:18 -0600 | [diff] [blame] | 568 | if (tcon->ses->server->ops->query_all_EAs == NULL) { |
| 569 | cifs_put_tlink(tlink); |
| 570 | return -EOPNOTSUPP; |
| 571 | } |
| 572 | |
| 573 | rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path, |
| 574 | "SETFILEBITS", ea_value, 4 /* size of buf */, |
Steve French | 67b4c88 | 2017-05-12 20:59:10 -0500 | [diff] [blame] | 575 | cifs_sb); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 576 | cifs_put_tlink(tlink); |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 577 | if (rc < 0) |
Steve French | 9e294f1 | 2005-11-17 16:59:21 -0800 | [diff] [blame] | 578 | return (int)rc; |
| 579 | else if (rc > 3) { |
| 580 | mode = le32_to_cpu(*((__le32 *)ea_value)); |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 581 | fattr->cf_mode &= ~SFBITS_MASK; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 582 | cifs_dbg(FYI, "special bits 0%o org mode 0%o\n", |
| 583 | mode, fattr->cf_mode); |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 584 | fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 585 | cifs_dbg(FYI, "special mode bits 0%o\n", mode); |
Steve French | 9e294f1 | 2005-11-17 16:59:21 -0800 | [diff] [blame] | 586 | } |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 587 | |
| 588 | return 0; |
Steve French | 3020a1f | 2005-11-18 11:31:10 -0800 | [diff] [blame] | 589 | #else |
| 590 | return -EOPNOTSUPP; |
| 591 | #endif |
Steve French | 9e294f1 | 2005-11-17 16:59:21 -0800 | [diff] [blame] | 592 | } |
| 593 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 594 | /* Fill a cifs_fattr struct with info from FILE_ALL_INFO */ |
Steve French | f1230c9 | 2009-07-22 23:13:01 +0000 | [diff] [blame] | 595 | static void |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 596 | cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info, |
Deepa Dinamani | e37fea5 | 2017-05-08 15:59:16 -0700 | [diff] [blame] | 597 | struct super_block *sb, bool adjust_tz, |
Pavel Shilovsky | eb85d94b | 2013-10-23 17:49:47 +0400 | [diff] [blame] | 598 | bool symlink) |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 599 | { |
Deepa Dinamani | e37fea5 | 2017-05-08 15:59:16 -0700 | [diff] [blame] | 600 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 601 | struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); |
Jeff Layton | 0d424ad | 2010-09-20 16:01:35 -0700 | [diff] [blame] | 602 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 603 | memset(fattr, 0, sizeof(*fattr)); |
| 604 | fattr->cf_cifsattrs = le32_to_cpu(info->Attributes); |
| 605 | if (info->DeletePending) |
| 606 | fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING; |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 607 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 608 | if (info->LastAccessTime) |
| 609 | fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime); |
Deepa Dinamani | e37fea5 | 2017-05-08 15:59:16 -0700 | [diff] [blame] | 610 | else { |
Arnd Bergmann | 9539020 | 2018-06-19 17:27:58 +0200 | [diff] [blame] | 611 | ktime_get_real_ts64(&fattr->cf_atime); |
| 612 | fattr->cf_atime = timespec64_trunc(fattr->cf_atime, sb->s_time_gran); |
Deepa Dinamani | e37fea5 | 2017-05-08 15:59:16 -0700 | [diff] [blame] | 613 | } |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 614 | |
| 615 | fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime); |
| 616 | fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime); |
| 617 | |
| 618 | if (adjust_tz) { |
Jeff Layton | 0d424ad | 2010-09-20 16:01:35 -0700 | [diff] [blame] | 619 | fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj; |
| 620 | fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj; |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | fattr->cf_eof = le64_to_cpu(info->EndOfFile); |
| 624 | fattr->cf_bytes = le64_to_cpu(info->AllocationSize); |
Jeff Layton | 20054bd | 2011-01-07 11:30:27 -0500 | [diff] [blame] | 625 | fattr->cf_createtime = le64_to_cpu(info->CreationTime); |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 626 | |
Jim McDonough | 74d290d | 2013-09-21 10:36:10 -0500 | [diff] [blame] | 627 | fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks); |
Pavel Shilovsky | eb85d94b | 2013-10-23 17:49:47 +0400 | [diff] [blame] | 628 | |
| 629 | if (symlink) { |
| 630 | fattr->cf_mode = S_IFLNK; |
| 631 | fattr->cf_dtype = DT_LNK; |
| 632 | } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) { |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 633 | fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode; |
| 634 | fattr->cf_dtype = DT_DIR; |
Pavel Shilovsky | 6de2ce4 | 2012-02-17 16:13:30 +0300 | [diff] [blame] | 635 | /* |
| 636 | * Server can return wrong NumberOfLinks value for directories |
| 637 | * when Unix extensions are disabled - fake it. |
| 638 | */ |
Jim McDonough | 74d290d | 2013-09-21 10:36:10 -0500 | [diff] [blame] | 639 | if (!tcon->unix_ext) |
| 640 | fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK; |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 641 | } else { |
| 642 | fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode; |
| 643 | fattr->cf_dtype = DT_REG; |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 644 | |
Jeff Layton | d0c280d | 2009-07-09 01:46:44 -0400 | [diff] [blame] | 645 | /* clear write bits if ATTR_READONLY is set */ |
| 646 | if (fattr->cf_cifsattrs & ATTR_READONLY) |
| 647 | fattr->cf_mode &= ~(S_IWUGO); |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 648 | |
Jim McDonough | 74d290d | 2013-09-21 10:36:10 -0500 | [diff] [blame] | 649 | /* |
| 650 | * Don't accept zero nlink from non-unix servers unless |
| 651 | * delete is pending. Instead mark it as unknown. |
| 652 | */ |
| 653 | if ((fattr->cf_nlink < 1) && !tcon->unix_ext && |
| 654 | !info->DeletePending) { |
| 655 | cifs_dbg(1, "bogus file nlink value %u\n", |
Steve French | 6658b9f | 2013-07-04 14:38:48 -0500 | [diff] [blame] | 656 | fattr->cf_nlink); |
Jim McDonough | 74d290d | 2013-09-21 10:36:10 -0500 | [diff] [blame] | 657 | fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK; |
Steve French | 6658b9f | 2013-07-04 14:38:48 -0500 | [diff] [blame] | 658 | } |
Pavel Shilovsky | 6de2ce4 | 2012-02-17 16:13:30 +0300 | [diff] [blame] | 659 | } |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 660 | |
| 661 | fattr->cf_uid = cifs_sb->mnt_uid; |
| 662 | fattr->cf_gid = cifs_sb->mnt_gid; |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Pavel Shilovsky | 4ad6504 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 665 | static int |
| 666 | cifs_get_file_info(struct file *filp) |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 667 | { |
| 668 | int rc; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 669 | unsigned int xid; |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 670 | FILE_ALL_INFO find_data; |
| 671 | struct cifs_fattr fattr; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 672 | struct inode *inode = file_inode(filp); |
Joe Perches | c21dfb6 | 2010-07-12 13:50:14 -0700 | [diff] [blame] | 673 | struct cifsFileInfo *cfile = filp->private_data; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 674 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
Pavel Shilovsky | 4ad6504 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 675 | struct TCP_Server_Info *server = tcon->ses->server; |
| 676 | |
| 677 | if (!server->ops->query_file_info) |
| 678 | return -ENOSYS; |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 679 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 680 | xid = get_xid(); |
Pavel Shilovsky | 4ad6504 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 681 | rc = server->ops->query_file_info(xid, tcon, &cfile->fid, &find_data); |
Pavel Shilovsky | 42274bb | 2011-10-22 14:37:50 +0400 | [diff] [blame] | 682 | switch (rc) { |
| 683 | case 0: |
Deepa Dinamani | e37fea5 | 2017-05-08 15:59:16 -0700 | [diff] [blame] | 684 | cifs_all_info_to_fattr(&fattr, &find_data, inode->i_sb, false, |
Pavel Shilovsky | eb85d94b | 2013-10-23 17:49:47 +0400 | [diff] [blame] | 685 | false); |
Pavel Shilovsky | 42274bb | 2011-10-22 14:37:50 +0400 | [diff] [blame] | 686 | break; |
| 687 | case -EREMOTE: |
| 688 | cifs_create_dfs_fattr(&fattr, inode->i_sb); |
| 689 | rc = 0; |
| 690 | break; |
| 691 | case -EOPNOTSUPP: |
| 692 | case -EINVAL: |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 693 | /* |
| 694 | * FIXME: legacy server -- fall back to path-based call? |
Steve French | ff215713 | 2010-03-09 20:30:42 +0000 | [diff] [blame] | 695 | * for now, just skip revalidating and mark inode for |
| 696 | * immediate reval. |
| 697 | */ |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 698 | rc = 0; |
| 699 | CIFS_I(inode)->time = 0; |
Pavel Shilovsky | 42274bb | 2011-10-22 14:37:50 +0400 | [diff] [blame] | 700 | default: |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 701 | goto cgfi_exit; |
Pavel Shilovsky | 42274bb | 2011-10-22 14:37:50 +0400 | [diff] [blame] | 702 | } |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 703 | |
| 704 | /* |
| 705 | * don't bother with SFU junk here -- just mark inode as needing |
| 706 | * revalidation. |
| 707 | */ |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 708 | fattr.cf_uniqueid = CIFS_I(inode)->uniqueid; |
| 709 | fattr.cf_flags |= CIFS_FATTR_NEED_REVAL; |
| 710 | cifs_fattr_to_inode(inode, &fattr); |
| 711 | cgfi_exit: |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 712 | free_xid(xid); |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 713 | return rc; |
| 714 | } |
| 715 | |
Steve French | 7ea884c | 2018-03-31 18:13:38 -0500 | [diff] [blame] | 716 | /* Simple function to return a 64 bit hash of string. Rarely called */ |
| 717 | static __u64 simple_hashstr(const char *str) |
| 718 | { |
Geert Uytterhoeven | 3995bbf | 2018-04-05 14:57:11 +0200 | [diff] [blame] | 719 | const __u64 hash_mult = 1125899906842597ULL; /* a big enough prime */ |
Steve French | 7ea884c | 2018-03-31 18:13:38 -0500 | [diff] [blame] | 720 | __u64 hash = 0; |
| 721 | |
| 722 | while (*str) |
| 723 | hash = (hash + (__u64) *str++) * hash_mult; |
| 724 | |
| 725 | return hash; |
| 726 | } |
| 727 | |
Pavel Shilovsky | 1208ef1 | 2012-05-27 17:34:43 +0400 | [diff] [blame] | 728 | int |
| 729 | cifs_get_inode_info(struct inode **inode, const char *full_path, |
| 730 | FILE_ALL_INFO *data, struct super_block *sb, int xid, |
Steve French | 42eacf9 | 2014-02-10 14:08:16 -0600 | [diff] [blame] | 731 | const struct cifs_fid *fid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | { |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 733 | __u16 srchflgs; |
| 734 | int rc = 0, tmprc = ENOSYS; |
Pavel Shilovsky | 1208ef1 | 2012-05-27 17:34:43 +0400 | [diff] [blame] | 735 | struct cifs_tcon *tcon; |
| 736 | struct TCP_Server_Info *server; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 737 | struct tcon_link *tlink; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | char *buf = NULL; |
Pavel Shilovsky | 1208ef1 | 2012-05-27 17:34:43 +0400 | [diff] [blame] | 740 | bool adjust_tz = false; |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 741 | struct cifs_fattr fattr; |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 742 | struct cifs_search_info *srchinf = NULL; |
Pavel Shilovsky | eb85d94b | 2013-10-23 17:49:47 +0400 | [diff] [blame] | 743 | bool symlink = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 745 | tlink = cifs_sb_tlink(cifs_sb); |
| 746 | if (IS_ERR(tlink)) |
| 747 | return PTR_ERR(tlink); |
Pavel Shilovsky | 1208ef1 | 2012-05-27 17:34:43 +0400 | [diff] [blame] | 748 | tcon = tlink_tcon(tlink); |
| 749 | server = tcon->ses->server; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 750 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 751 | cifs_dbg(FYI, "Getting info on %s\n", full_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
Pavel Shilovsky | 1208ef1 | 2012-05-27 17:34:43 +0400 | [diff] [blame] | 753 | if ((data == NULL) && (*inode != NULL)) { |
Ronnie Sahlberg | 57c55cd | 2018-05-24 06:54:27 +1000 | [diff] [blame] | 754 | if (CIFS_CACHE_READ(CIFS_I(*inode)) && |
| 755 | CIFS_I(*inode)->time != 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 756 | cifs_dbg(FYI, "No need to revalidate cached inode sizes\n"); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 757 | goto cgii_exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | |
Pavel Shilovsky | 1208ef1 | 2012-05-27 17:34:43 +0400 | [diff] [blame] | 761 | /* if inode info is not passed, get it from server */ |
| 762 | if (data == NULL) { |
| 763 | if (!server->ops->query_path_info) { |
| 764 | rc = -ENOSYS; |
| 765 | goto cgii_exit; |
| 766 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 768 | if (buf == NULL) { |
| 769 | rc = -ENOMEM; |
| 770 | goto cgii_exit; |
| 771 | } |
Pavel Shilovsky | 1208ef1 | 2012-05-27 17:34:43 +0400 | [diff] [blame] | 772 | data = (FILE_ALL_INFO *)buf; |
| 773 | rc = server->ops->query_path_info(xid, tcon, cifs_sb, full_path, |
Pavel Shilovsky | eb85d94b | 2013-10-23 17:49:47 +0400 | [diff] [blame] | 774 | data, &adjust_tz, &symlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | } |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 776 | |
| 777 | if (!rc) { |
Deepa Dinamani | e37fea5 | 2017-05-08 15:59:16 -0700 | [diff] [blame] | 778 | cifs_all_info_to_fattr(&fattr, data, sb, adjust_tz, |
Pavel Shilovsky | eb85d94b | 2013-10-23 17:49:47 +0400 | [diff] [blame] | 779 | symlink); |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 780 | } else if (rc == -EREMOTE) { |
| 781 | cifs_create_dfs_fattr(&fattr, sb); |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 782 | rc = 0; |
Steve French | 1e77a8c | 2018-10-19 00:45:21 -0500 | [diff] [blame] | 783 | } else if ((rc == -EACCES) && backup_cred(cifs_sb) && |
| 784 | (strcmp(server->vals->version_string, SMB1_VERSION_STRING) |
| 785 | == 0)) { |
Steve French | 3b7960c | 2018-10-19 01:58:22 -0500 | [diff] [blame] | 786 | /* |
| 787 | * For SMB2 and later the backup intent flag is already |
| 788 | * sent if needed on open and there is no path based |
| 789 | * FindFirst operation to use to retry with |
| 790 | */ |
Steve French | 1e77a8c | 2018-10-19 00:45:21 -0500 | [diff] [blame] | 791 | |
Steve French | 3b7960c | 2018-10-19 01:58:22 -0500 | [diff] [blame] | 792 | srchinf = kzalloc(sizeof(struct cifs_search_info), |
| 793 | GFP_KERNEL); |
| 794 | if (srchinf == NULL) { |
| 795 | rc = -ENOMEM; |
| 796 | goto cgii_exit; |
| 797 | } |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 798 | |
Steve French | 3b7960c | 2018-10-19 01:58:22 -0500 | [diff] [blame] | 799 | srchinf->endOfSearch = false; |
| 800 | if (tcon->unix_ext) |
| 801 | srchinf->info_level = SMB_FIND_FILE_UNIX; |
| 802 | else if ((tcon->ses->capabilities & |
| 803 | tcon->ses->server->vals->cap_nt_find) == 0) |
| 804 | srchinf->info_level = SMB_FIND_FILE_INFO_STANDARD; |
| 805 | else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 806 | srchinf->info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO; |
Steve French | 3b7960c | 2018-10-19 01:58:22 -0500 | [diff] [blame] | 807 | else /* no srvino useful for fallback to some netapp */ |
| 808 | srchinf->info_level = SMB_FIND_FILE_DIRECTORY_INFO; |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 809 | |
Steve French | 3b7960c | 2018-10-19 01:58:22 -0500 | [diff] [blame] | 810 | srchflgs = CIFS_SEARCH_CLOSE_ALWAYS | |
| 811 | CIFS_SEARCH_CLOSE_AT_END | |
| 812 | CIFS_SEARCH_BACKUP_SEARCH; |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 813 | |
Steve French | 3b7960c | 2018-10-19 01:58:22 -0500 | [diff] [blame] | 814 | rc = CIFSFindFirst(xid, tcon, full_path, |
| 815 | cifs_sb, NULL, srchflgs, srchinf, false); |
| 816 | if (!rc) { |
| 817 | data = (FILE_ALL_INFO *)srchinf->srch_entries_start; |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 818 | |
Steve French | 3b7960c | 2018-10-19 01:58:22 -0500 | [diff] [blame] | 819 | cifs_dir_info_to_fattr(&fattr, |
| 820 | (FILE_DIRECTORY_INFO *)data, cifs_sb); |
| 821 | fattr.cf_uniqueid = le64_to_cpu( |
| 822 | ((SEARCH_ID_FULL_DIR_INFO *)data)->UniqueId); |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 823 | |
Steve French | 3b7960c | 2018-10-19 01:58:22 -0500 | [diff] [blame] | 824 | cifs_buf_release(srchinf->ntwrk_buf_start); |
| 825 | } |
| 826 | kfree(srchinf); |
| 827 | if (rc) |
| 828 | goto cgii_exit; |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 829 | } else |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 830 | goto cgii_exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 832 | /* |
| 833 | * If an inode wasn't passed in, then get the inode number |
| 834 | * |
| 835 | * Is an i_ino of zero legal? Can we use that to check if the server |
| 836 | * supports returning inode numbers? Are there other sanity checks we |
| 837 | * can use to ensure that the server is really filling in that field? |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 838 | */ |
Pavel Shilovsky | 1208ef1 | 2012-05-27 17:34:43 +0400 | [diff] [blame] | 839 | if (*inode == NULL) { |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 840 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) { |
Aurelien Aptel | 5fc7fcd | 2018-11-16 16:13:25 +0100 | [diff] [blame] | 841 | if (server->ops->get_srv_inum) |
| 842 | tmprc = server->ops->get_srv_inum(xid, |
| 843 | tcon, cifs_sb, full_path, |
| 844 | &fattr.cf_uniqueid, data); |
| 845 | if (tmprc) { |
| 846 | cifs_dbg(FYI, "GetSrvInodeNum rc %d\n", |
| 847 | tmprc); |
| 848 | fattr.cf_uniqueid = iunique(sb, ROOT_I); |
| 849 | cifs_autodisable_serverino(cifs_sb); |
| 850 | } else if ((fattr.cf_uniqueid == 0) && |
| 851 | strlen(full_path) == 0) { |
| 852 | /* some servers ret bad root ino ie 0 */ |
| 853 | cifs_dbg(FYI, "Invalid (0) inodenum\n"); |
| 854 | fattr.cf_flags |= |
| 855 | CIFS_FATTR_FAKE_ROOT_INO; |
| 856 | fattr.cf_uniqueid = |
| 857 | simple_hashstr(tcon->treeName); |
Jeff Layton | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 858 | } |
Shirish Pargaonkar | c052e2b | 2012-09-28 12:21:14 -0500 | [diff] [blame] | 859 | } else |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 860 | fattr.cf_uniqueid = iunique(sb, ROOT_I); |
Ross Lagerwall | a108471b | 2015-12-02 14:46:08 +0000 | [diff] [blame] | 861 | } else { |
Aurelien Aptel | 5fc7fcd | 2018-11-16 16:13:25 +0100 | [diff] [blame] | 862 | if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) |
| 863 | && server->ops->get_srv_inum) { |
Ross Lagerwall | a108471b | 2015-12-02 14:46:08 +0000 | [diff] [blame] | 864 | /* |
| 865 | * Pass a NULL tcon to ensure we don't make a round |
| 866 | * trip to the server. This only works for SMB2+. |
| 867 | */ |
| 868 | tmprc = server->ops->get_srv_inum(xid, |
| 869 | NULL, cifs_sb, full_path, |
| 870 | &fattr.cf_uniqueid, data); |
| 871 | if (tmprc) |
| 872 | fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid; |
Steve French | 7ea884c | 2018-03-31 18:13:38 -0500 | [diff] [blame] | 873 | else if ((fattr.cf_uniqueid == 0) && |
| 874 | strlen(full_path) == 0) { |
| 875 | /* |
| 876 | * Reuse existing root inode num since |
| 877 | * inum zero for root causes ls of . and .. to |
| 878 | * not be returned |
| 879 | */ |
| 880 | cifs_dbg(FYI, "Srv ret 0 inode num for root\n"); |
| 881 | fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid; |
| 882 | } |
Ross Lagerwall | a108471b | 2015-12-02 14:46:08 +0000 | [diff] [blame] | 883 | } else |
| 884 | fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid; |
| 885 | } |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 886 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 887 | /* query for SFU type info if supported and needed */ |
| 888 | if (fattr.cf_cifsattrs & ATTR_SYSTEM && |
| 889 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) { |
| 890 | tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid); |
| 891 | if (tmprc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 892 | cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc); |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 893 | } |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 894 | |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 895 | #ifdef CONFIG_CIFS_ACL |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 896 | /* fill in 0777 bits from ACL */ |
| 897 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) { |
Pavel Shilovsky | 1208ef1 | 2012-05-27 17:34:43 +0400 | [diff] [blame] | 898 | rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, full_path, fid); |
Shirish Pargaonkar | 78415d2 | 2010-11-27 11:37:26 -0600 | [diff] [blame] | 899 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 900 | cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n", |
| 901 | __func__, rc); |
Shirish Pargaonkar | 78415d2 | 2010-11-27 11:37:26 -0600 | [diff] [blame] | 902 | goto cgii_exit; |
| 903 | } |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 904 | } |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 905 | #endif /* CONFIG_CIFS_ACL */ |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 906 | |
| 907 | /* fill in remaining high mode bits e.g. SUID, VTX */ |
| 908 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) |
| 909 | cifs_sfu_mode(&fattr, full_path, cifs_sb, xid); |
| 910 | |
Stefan Metzmacher | 1b12b9c | 2010-08-05 21:19:56 +0200 | [diff] [blame] | 911 | /* check for Minshall+French symlinks */ |
| 912 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) { |
Sachin Prabhu | cb084b1 | 2013-11-25 17:09:50 +0000 | [diff] [blame] | 913 | tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr, |
| 914 | full_path); |
Stefan Metzmacher | 1b12b9c | 2010-08-05 21:19:56 +0200 | [diff] [blame] | 915 | if (tmprc) |
Sachin Prabhu | cb084b1 | 2013-11-25 17:09:50 +0000 | [diff] [blame] | 916 | cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc); |
Stefan Metzmacher | 1b12b9c | 2010-08-05 21:19:56 +0200 | [diff] [blame] | 917 | } |
| 918 | |
Pavel Shilovsky | 1208ef1 | 2012-05-27 17:34:43 +0400 | [diff] [blame] | 919 | if (!*inode) { |
| 920 | *inode = cifs_iget(sb, &fattr); |
| 921 | if (!*inode) |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 922 | rc = -ENOMEM; |
| 923 | } else { |
Nakajima Akira | 7196ac1 | 2015-04-22 15:24:44 +0900 | [diff] [blame] | 924 | /* we already have inode, update it */ |
| 925 | |
Ross Lagerwall | a108471b | 2015-12-02 14:46:08 +0000 | [diff] [blame] | 926 | /* if uniqueid is different, return error */ |
| 927 | if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM && |
| 928 | CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) { |
| 929 | rc = -ESTALE; |
| 930 | goto cgii_exit; |
| 931 | } |
| 932 | |
Nakajima Akira | 7196ac1 | 2015-04-22 15:24:44 +0900 | [diff] [blame] | 933 | /* if filetype is different, return error */ |
| 934 | if (unlikely(((*inode)->i_mode & S_IFMT) != |
| 935 | (fattr.cf_mode & S_IFMT))) { |
| 936 | rc = -ESTALE; |
| 937 | goto cgii_exit; |
| 938 | } |
| 939 | |
Pavel Shilovsky | 1208ef1 | 2012-05-27 17:34:43 +0400 | [diff] [blame] | 940 | cifs_fattr_to_inode(*inode, &fattr); |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 943 | cgii_exit: |
Steve French | 7ea884c | 2018-03-31 18:13:38 -0500 | [diff] [blame] | 944 | if ((*inode) && ((*inode)->i_ino == 0)) |
| 945 | cifs_dbg(FYI, "inode number of zero returned\n"); |
| 946 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | kfree(buf); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 948 | cifs_put_tlink(tlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | return rc; |
| 950 | } |
| 951 | |
Steve French | 7f8ed42 | 2007-09-28 22:28:55 +0000 | [diff] [blame] | 952 | static const struct inode_operations cifs_ipc_inode_ops = { |
| 953 | .lookup = cifs_lookup, |
| 954 | }; |
| 955 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 956 | static int |
| 957 | cifs_find_inode(struct inode *inode, void *opaque) |
| 958 | { |
| 959 | struct cifs_fattr *fattr = (struct cifs_fattr *) opaque; |
| 960 | |
Jeff Layton | f30b9c1 | 2010-07-19 18:00:17 -0400 | [diff] [blame] | 961 | /* don't match inode with different uniqueid */ |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 962 | if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid) |
| 963 | return 0; |
| 964 | |
Jeff Layton | 20054bd | 2011-01-07 11:30:27 -0500 | [diff] [blame] | 965 | /* use createtime like an i_generation field */ |
| 966 | if (CIFS_I(inode)->createtime != fattr->cf_createtime) |
| 967 | return 0; |
| 968 | |
Jeff Layton | f30b9c1 | 2010-07-19 18:00:17 -0400 | [diff] [blame] | 969 | /* don't match inode of different type */ |
| 970 | if ((inode->i_mode & S_IFMT) != (fattr->cf_mode & S_IFMT)) |
| 971 | return 0; |
| 972 | |
Jeff Layton | 5acfec2 | 2010-08-02 17:43:54 -0400 | [diff] [blame] | 973 | /* if it's not a directory or has no dentries, then flag it */ |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 974 | if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry)) |
Jeff Layton | 3d69438 | 2010-05-11 14:59:55 -0400 | [diff] [blame] | 975 | fattr->cf_flags |= CIFS_FATTR_INO_COLLISION; |
Jeff Layton | 3d69438 | 2010-05-11 14:59:55 -0400 | [diff] [blame] | 976 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 977 | return 1; |
| 978 | } |
| 979 | |
| 980 | static int |
| 981 | cifs_init_inode(struct inode *inode, void *opaque) |
| 982 | { |
| 983 | struct cifs_fattr *fattr = (struct cifs_fattr *) opaque; |
| 984 | |
| 985 | CIFS_I(inode)->uniqueid = fattr->cf_uniqueid; |
Jeff Layton | 20054bd | 2011-01-07 11:30:27 -0500 | [diff] [blame] | 986 | CIFS_I(inode)->createtime = fattr->cf_createtime; |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 987 | return 0; |
| 988 | } |
| 989 | |
Jeff Layton | 5acfec2 | 2010-08-02 17:43:54 -0400 | [diff] [blame] | 990 | /* |
| 991 | * walk dentry list for an inode and report whether it has aliases that |
| 992 | * are hashed. We use this to determine if a directory inode can actually |
| 993 | * be used. |
| 994 | */ |
| 995 | static bool |
| 996 | inode_has_hashed_dentries(struct inode *inode) |
| 997 | { |
| 998 | struct dentry *dentry; |
| 999 | |
Nick Piggin | 873feea | 2011-01-07 17:50:06 +1100 | [diff] [blame] | 1000 | spin_lock(&inode->i_lock); |
Al Viro | 946e51f | 2014-10-26 19:19:16 -0400 | [diff] [blame] | 1001 | hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) { |
Jeff Layton | 5acfec2 | 2010-08-02 17:43:54 -0400 | [diff] [blame] | 1002 | if (!d_unhashed(dentry) || IS_ROOT(dentry)) { |
Nick Piggin | 873feea | 2011-01-07 17:50:06 +1100 | [diff] [blame] | 1003 | spin_unlock(&inode->i_lock); |
Jeff Layton | 5acfec2 | 2010-08-02 17:43:54 -0400 | [diff] [blame] | 1004 | return true; |
| 1005 | } |
| 1006 | } |
Nick Piggin | 873feea | 2011-01-07 17:50:06 +1100 | [diff] [blame] | 1007 | spin_unlock(&inode->i_lock); |
Jeff Layton | 5acfec2 | 2010-08-02 17:43:54 -0400 | [diff] [blame] | 1008 | return false; |
| 1009 | } |
| 1010 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 1011 | /* Given fattrs, get a corresponding inode */ |
| 1012 | struct inode * |
| 1013 | cifs_iget(struct super_block *sb, struct cifs_fattr *fattr) |
| 1014 | { |
| 1015 | unsigned long hash; |
| 1016 | struct inode *inode; |
| 1017 | |
Jeff Layton | 3d69438 | 2010-05-11 14:59:55 -0400 | [diff] [blame] | 1018 | retry_iget5_locked: |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1019 | cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid); |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 1020 | |
| 1021 | /* hash down to 32-bits on 32-bit arch */ |
| 1022 | hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid); |
| 1023 | |
| 1024 | inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr); |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 1025 | if (inode) { |
Jeff Layton | 5acfec2 | 2010-08-02 17:43:54 -0400 | [diff] [blame] | 1026 | /* was there a potentially problematic inode collision? */ |
Jeff Layton | 3d69438 | 2010-05-11 14:59:55 -0400 | [diff] [blame] | 1027 | if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) { |
Jeff Layton | 3d69438 | 2010-05-11 14:59:55 -0400 | [diff] [blame] | 1028 | fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION; |
Jeff Layton | 5acfec2 | 2010-08-02 17:43:54 -0400 | [diff] [blame] | 1029 | |
| 1030 | if (inode_has_hashed_dentries(inode)) { |
| 1031 | cifs_autodisable_serverino(CIFS_SB(sb)); |
| 1032 | iput(inode); |
| 1033 | fattr->cf_uniqueid = iunique(sb, ROOT_I); |
| 1034 | goto retry_iget5_locked; |
| 1035 | } |
Jeff Layton | 3d69438 | 2010-05-11 14:59:55 -0400 | [diff] [blame] | 1036 | } |
| 1037 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 1038 | cifs_fattr_to_inode(inode, fattr); |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 1039 | if (sb->s_flags & SB_NOATIME) |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 1040 | inode->i_flags |= S_NOATIME | S_NOCMTIME; |
| 1041 | if (inode->i_state & I_NEW) { |
| 1042 | inode->i_ino = hash; |
Steve French | 0ccd480 | 2010-07-16 04:31:02 +0000 | [diff] [blame] | 1043 | #ifdef CONFIG_CIFS_FSCACHE |
Suresh Jayaraman | 9451a9a | 2010-07-05 18:12:45 +0530 | [diff] [blame] | 1044 | /* initialize per-inode cache cookie pointer */ |
| 1045 | CIFS_I(inode)->fscache = NULL; |
Steve French | 0ccd480 | 2010-07-16 04:31:02 +0000 | [diff] [blame] | 1046 | #endif |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 1047 | unlock_new_inode(inode); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | return inode; |
| 1052 | } |
| 1053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | /* gets root inode */ |
Shirish Pargaonkar | 9b6763e | 2011-02-21 23:56:59 -0600 | [diff] [blame] | 1055 | struct inode *cifs_root_iget(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1057 | unsigned int xid; |
Jeff Layton | 0d424ad | 2010-09-20 16:01:35 -0700 | [diff] [blame] | 1058 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 1059 | struct inode *inode = NULL; |
David Howells | ce634ab | 2008-02-07 00:15:33 -0800 | [diff] [blame] | 1060 | long rc; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1061 | struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 1062 | char *path = NULL; |
| 1063 | int len; |
| 1064 | |
| 1065 | if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) |
| 1066 | && cifs_sb->prepath) { |
| 1067 | len = strlen(cifs_sb->prepath); |
| 1068 | path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL); |
| 1069 | if (path == NULL) |
| 1070 | return ERR_PTR(-ENOMEM); |
| 1071 | path[0] = '/'; |
| 1072 | memcpy(path+1, cifs_sb->prepath, len); |
| 1073 | } else { |
| 1074 | path = kstrdup("", GFP_KERNEL); |
| 1075 | if (path == NULL) |
| 1076 | return ERR_PTR(-ENOMEM); |
| 1077 | } |
David Howells | ce634ab | 2008-02-07 00:15:33 -0800 | [diff] [blame] | 1078 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1079 | xid = get_xid(); |
Steve French | b5b374e | 2014-10-06 01:01:03 -0500 | [diff] [blame] | 1080 | if (tcon->unix_ext) { |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 1081 | rc = cifs_get_inode_info_unix(&inode, path, sb, xid); |
Steve French | b5b374e | 2014-10-06 01:01:03 -0500 | [diff] [blame] | 1082 | /* some servers mistakenly claim POSIX support */ |
| 1083 | if (rc != -EOPNOTSUPP) |
| 1084 | goto iget_no_retry; |
| 1085 | cifs_dbg(VFS, "server does not support POSIX extensions"); |
| 1086 | tcon->unix_ext = false; |
| 1087 | } |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 1088 | |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 1089 | convert_delimiter(path, CIFS_DIR_SEP(cifs_sb)); |
| 1090 | rc = cifs_get_inode_info(&inode, path, NULL, sb, xid, NULL); |
Steve French | b5b374e | 2014-10-06 01:01:03 -0500 | [diff] [blame] | 1091 | |
| 1092 | iget_no_retry: |
Oskar Schirmer | a7851ce | 2010-11-10 21:06:13 +0000 | [diff] [blame] | 1093 | if (!inode) { |
| 1094 | inode = ERR_PTR(rc); |
| 1095 | goto out; |
| 1096 | } |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 1097 | |
Steve French | 0ccd480 | 2010-07-16 04:31:02 +0000 | [diff] [blame] | 1098 | #ifdef CONFIG_CIFS_FSCACHE |
Suresh Jayaraman | d03382c | 2010-07-05 18:12:27 +0530 | [diff] [blame] | 1099 | /* populate tcon->resource_id */ |
Jeff Layton | 0d424ad | 2010-09-20 16:01:35 -0700 | [diff] [blame] | 1100 | tcon->resource_id = CIFS_I(inode)->uniqueid; |
Steve French | 0ccd480 | 2010-07-16 04:31:02 +0000 | [diff] [blame] | 1101 | #endif |
Suresh Jayaraman | d03382c | 2010-07-05 18:12:27 +0530 | [diff] [blame] | 1102 | |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1103 | if (rc && tcon->pipe) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1104 | cifs_dbg(FYI, "ipc connection - fake read inode\n"); |
Steve French | b7ca692 | 2012-08-03 08:43:01 -0500 | [diff] [blame] | 1105 | spin_lock(&inode->i_lock); |
Steve French | 7f8ed42 | 2007-09-28 22:28:55 +0000 | [diff] [blame] | 1106 | inode->i_mode |= S_IFDIR; |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 1107 | set_nlink(inode, 2); |
Steve French | 7f8ed42 | 2007-09-28 22:28:55 +0000 | [diff] [blame] | 1108 | inode->i_op = &cifs_ipc_inode_ops; |
| 1109 | inode->i_fop = &simple_dir_operations; |
| 1110 | inode->i_uid = cifs_sb->mnt_uid; |
| 1111 | inode->i_gid = cifs_sb->mnt_gid; |
Steve French | b7ca692 | 2012-08-03 08:43:01 -0500 | [diff] [blame] | 1112 | spin_unlock(&inode->i_lock); |
Steve French | ad66133 | 2008-08-12 14:14:40 +0000 | [diff] [blame] | 1113 | } else if (rc) { |
David Howells | ce634ab | 2008-02-07 00:15:33 -0800 | [diff] [blame] | 1114 | iget_failed(inode); |
Oskar Schirmer | a7851ce | 2010-11-10 21:06:13 +0000 | [diff] [blame] | 1115 | inode = ERR_PTR(rc); |
Steve French | 7f8ed42 | 2007-09-28 22:28:55 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Oskar Schirmer | a7851ce | 2010-11-10 21:06:13 +0000 | [diff] [blame] | 1118 | out: |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 1119 | kfree(path); |
Phillip Potter | 31cd106 | 2018-03-17 21:06:56 +0000 | [diff] [blame] | 1120 | free_xid(xid); |
David Howells | ce634ab | 2008-02-07 00:15:33 -0800 | [diff] [blame] | 1121 | return inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | } |
| 1123 | |
Pavel Shilovsky | ed6875e | 2012-09-18 16:20:25 -0700 | [diff] [blame] | 1124 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1125 | cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid, |
Pavel Shilovsky | ed6875e | 2012-09-18 16:20:25 -0700 | [diff] [blame] | 1126 | char *full_path, __u32 dosattr) |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 1127 | { |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 1128 | bool set_time = false; |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 1129 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
Pavel Shilovsky | 6bdf6db | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 1130 | struct TCP_Server_Info *server; |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 1131 | FILE_BASIC_INFO info_buf; |
| 1132 | |
Steve French | 1adcb71 | 2009-02-25 14:19:56 +0000 | [diff] [blame] | 1133 | if (attrs == NULL) |
| 1134 | return -EINVAL; |
| 1135 | |
Pavel Shilovsky | 6bdf6db | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 1136 | server = cifs_sb_master_tcon(cifs_sb)->ses->server; |
| 1137 | if (!server->ops->set_file_info) |
| 1138 | return -ENOSYS; |
| 1139 | |
Steve French | fd09b7d | 2018-08-02 20:28:18 -0500 | [diff] [blame] | 1140 | info_buf.Pad = 0; |
| 1141 | |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 1142 | if (attrs->ia_valid & ATTR_ATIME) { |
| 1143 | set_time = true; |
| 1144 | info_buf.LastAccessTime = |
Arnd Bergmann | 9539020 | 2018-06-19 17:27:58 +0200 | [diff] [blame] | 1145 | cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime)); |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 1146 | } else |
| 1147 | info_buf.LastAccessTime = 0; |
| 1148 | |
| 1149 | if (attrs->ia_valid & ATTR_MTIME) { |
| 1150 | set_time = true; |
| 1151 | info_buf.LastWriteTime = |
Arnd Bergmann | 9539020 | 2018-06-19 17:27:58 +0200 | [diff] [blame] | 1152 | cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime)); |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 1153 | } else |
| 1154 | info_buf.LastWriteTime = 0; |
| 1155 | |
| 1156 | /* |
| 1157 | * Samba throws this field away, but windows may actually use it. |
| 1158 | * Do not set ctime unless other time stamps are changed explicitly |
| 1159 | * (i.e. by utimes()) since we would then have a mix of client and |
| 1160 | * server times. |
| 1161 | */ |
| 1162 | if (set_time && (attrs->ia_valid & ATTR_CTIME)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1163 | cifs_dbg(FYI, "CIFS - CTIME changed\n"); |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 1164 | info_buf.ChangeTime = |
Arnd Bergmann | 9539020 | 2018-06-19 17:27:58 +0200 | [diff] [blame] | 1165 | cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime)); |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 1166 | } else |
| 1167 | info_buf.ChangeTime = 0; |
| 1168 | |
| 1169 | info_buf.CreationTime = 0; /* don't change */ |
| 1170 | info_buf.Attributes = cpu_to_le32(dosattr); |
| 1171 | |
Pavel Shilovsky | 6bdf6db | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 1172 | return server->ops->set_file_info(inode, full_path, &info_buf, xid); |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 1175 | /* |
Pavel Shilovsky | ed6875e | 2012-09-18 16:20:25 -0700 | [diff] [blame] | 1176 | * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 1177 | * and rename it to a random name that hopefully won't conflict with |
| 1178 | * anything else. |
| 1179 | */ |
Pavel Shilovsky | ed6875e | 2012-09-18 16:20:25 -0700 | [diff] [blame] | 1180 | int |
| 1181 | cifs_rename_pending_delete(const char *full_path, struct dentry *dentry, |
| 1182 | const unsigned int xid) |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 1183 | { |
| 1184 | int oplock = 0; |
| 1185 | int rc; |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 1186 | struct cifs_fid fid; |
| 1187 | struct cifs_open_parms oparms; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1188 | struct inode *inode = d_inode(dentry); |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 1189 | struct cifsInodeInfo *cifsInode = CIFS_I(inode); |
| 1190 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1191 | struct tcon_link *tlink; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1192 | struct cifs_tcon *tcon; |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 1193 | __u32 dosattr, origattr; |
| 1194 | FILE_BASIC_INFO *info_buf = NULL; |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 1195 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1196 | tlink = cifs_sb_tlink(cifs_sb); |
| 1197 | if (IS_ERR(tlink)) |
| 1198 | return PTR_ERR(tlink); |
| 1199 | tcon = tlink_tcon(tlink); |
| 1200 | |
Sachin Prabhu | c483a98 | 2013-03-05 19:25:56 +0000 | [diff] [blame] | 1201 | /* |
| 1202 | * We cannot rename the file if the server doesn't support |
| 1203 | * CAP_INFOLEVEL_PASSTHRU |
| 1204 | */ |
| 1205 | if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) { |
| 1206 | rc = -EBUSY; |
| 1207 | goto out; |
| 1208 | } |
| 1209 | |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 1210 | oparms.tcon = tcon; |
| 1211 | oparms.cifs_sb = cifs_sb; |
| 1212 | oparms.desired_access = DELETE | FILE_WRITE_ATTRIBUTES; |
| 1213 | oparms.create_options = CREATE_NOT_DIR; |
| 1214 | oparms.disposition = FILE_OPEN; |
| 1215 | oparms.path = full_path; |
| 1216 | oparms.fid = &fid; |
| 1217 | oparms.reconnect = false; |
| 1218 | |
| 1219 | rc = CIFS_open(xid, &oparms, &oplock, NULL); |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 1220 | if (rc != 0) |
| 1221 | goto out; |
| 1222 | |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 1223 | origattr = cifsInode->cifsAttrs; |
| 1224 | if (origattr == 0) |
| 1225 | origattr |= ATTR_NORMAL; |
| 1226 | |
| 1227 | dosattr = origattr & ~ATTR_READONLY; |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 1228 | if (dosattr == 0) |
| 1229 | dosattr |= ATTR_NORMAL; |
| 1230 | dosattr |= ATTR_HIDDEN; |
| 1231 | |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 1232 | /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */ |
| 1233 | if (dosattr != origattr) { |
| 1234 | info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL); |
| 1235 | if (info_buf == NULL) { |
| 1236 | rc = -ENOMEM; |
| 1237 | goto out_close; |
| 1238 | } |
| 1239 | info_buf->Attributes = cpu_to_le32(dosattr); |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 1240 | rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid, |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 1241 | current->tgid); |
| 1242 | /* although we would like to mark the file hidden |
| 1243 | if that fails we will still try to rename it */ |
Sachin Prabhu | 72d282d | 2013-03-05 19:25:55 +0000 | [diff] [blame] | 1244 | if (!rc) |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 1245 | cifsInode->cifsAttrs = dosattr; |
| 1246 | else |
| 1247 | dosattr = origattr; /* since not able to change them */ |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 1248 | } |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 1249 | |
Jeff Layton | dd1db2d | 2008-10-16 19:27:12 -0400 | [diff] [blame] | 1250 | /* rename the file */ |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 1251 | rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL, |
| 1252 | cifs_sb->local_nls, |
Steve French | 2baa268 | 2014-09-27 02:19:01 -0500 | [diff] [blame] | 1253 | cifs_remap(cifs_sb)); |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 1254 | if (rc != 0) { |
Sachin Prabhu | 47c78f4 | 2013-03-11 13:08:49 +0000 | [diff] [blame] | 1255 | rc = -EBUSY; |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 1256 | goto undo_setattr; |
| 1257 | } |
Jeff Layton | 6d22f09 | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 1258 | |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 1259 | /* try to set DELETE_ON_CLOSE */ |
Jeff Layton | aff8d5c | 2014-04-30 09:31:45 -0400 | [diff] [blame] | 1260 | if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) { |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 1261 | rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid, |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 1262 | current->tgid); |
| 1263 | /* |
| 1264 | * some samba versions return -ENOENT when we try to set the |
| 1265 | * file disposition here. Likely a samba bug, but work around |
| 1266 | * it for now. This means that some cifsXXX files may hang |
| 1267 | * around after they shouldn't. |
| 1268 | * |
| 1269 | * BB: remove this hack after more servers have the fix |
| 1270 | */ |
| 1271 | if (rc == -ENOENT) |
| 1272 | rc = 0; |
| 1273 | else if (rc != 0) { |
Sachin Prabhu | 47c78f4 | 2013-03-11 13:08:49 +0000 | [diff] [blame] | 1274 | rc = -EBUSY; |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 1275 | goto undo_rename; |
| 1276 | } |
Jeff Layton | aff8d5c | 2014-04-30 09:31:45 -0400 | [diff] [blame] | 1277 | set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags); |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 1278 | } |
Jeff Layton | 7ce86d5 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 1279 | |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 1280 | out_close: |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 1281 | CIFSSMBClose(xid, tcon, fid.netfid); |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 1282 | out: |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 1283 | kfree(info_buf); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1284 | cifs_put_tlink(tlink); |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 1285 | return rc; |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 1286 | |
| 1287 | /* |
| 1288 | * reset everything back to the original state. Don't bother |
| 1289 | * dealing with errors here since we can't do anything about |
| 1290 | * them anyway. |
| 1291 | */ |
| 1292 | undo_rename: |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 1293 | CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name, |
Steve French | 2baa268 | 2014-09-27 02:19:01 -0500 | [diff] [blame] | 1294 | cifs_sb->local_nls, cifs_remap(cifs_sb)); |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 1295 | undo_setattr: |
| 1296 | if (dosattr != origattr) { |
| 1297 | info_buf->Attributes = cpu_to_le32(origattr); |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 1298 | if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid, |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 1299 | current->tgid)) |
| 1300 | cifsInode->cifsAttrs = origattr; |
| 1301 | } |
| 1302 | |
| 1303 | goto out_close; |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 1304 | } |
| 1305 | |
Steve French | b7ca692 | 2012-08-03 08:43:01 -0500 | [diff] [blame] | 1306 | /* copied from fs/nfs/dir.c with small changes */ |
| 1307 | static void |
| 1308 | cifs_drop_nlink(struct inode *inode) |
| 1309 | { |
| 1310 | spin_lock(&inode->i_lock); |
| 1311 | if (inode->i_nlink > 0) |
| 1312 | drop_nlink(inode); |
| 1313 | spin_unlock(&inode->i_lock); |
| 1314 | } |
Steve French | ff69452 | 2009-04-20 19:45:13 +0000 | [diff] [blame] | 1315 | |
| 1316 | /* |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1317 | * If d_inode(dentry) is null (usually meaning the cached dentry |
Steve French | ff69452 | 2009-04-20 19:45:13 +0000 | [diff] [blame] | 1318 | * is a negative dentry) then we would attempt a standard SMB delete, but |
Colin Ian King | 413d610 | 2018-10-26 19:07:21 +0100 | [diff] [blame] | 1319 | * if that fails we can not attempt the fall back mechanisms on EACCES |
| 1320 | * but will return the EACCES to the caller. Note that the VFS does not call |
Steve French | ff69452 | 2009-04-20 19:45:13 +0000 | [diff] [blame] | 1321 | * unlink on negative dentries currently. |
| 1322 | */ |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1323 | int cifs_unlink(struct inode *dir, struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | { |
| 1325 | int rc = 0; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1326 | unsigned int xid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | char *full_path = NULL; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1328 | struct inode *inode = d_inode(dentry); |
Steve French | ff69452 | 2009-04-20 19:45:13 +0000 | [diff] [blame] | 1329 | struct cifsInodeInfo *cifs_inode; |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1330 | struct super_block *sb = dir->i_sb; |
| 1331 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1332 | struct tcon_link *tlink; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1333 | struct cifs_tcon *tcon; |
Pavel Shilovsky | ed6875e | 2012-09-18 16:20:25 -0700 | [diff] [blame] | 1334 | struct TCP_Server_Info *server; |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 1335 | struct iattr *attrs = NULL; |
| 1336 | __u32 dosattr = 0, origattr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1338 | cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1340 | tlink = cifs_sb_tlink(cifs_sb); |
| 1341 | if (IS_ERR(tlink)) |
| 1342 | return PTR_ERR(tlink); |
| 1343 | tcon = tlink_tcon(tlink); |
Pavel Shilovsky | ed6875e | 2012-09-18 16:20:25 -0700 | [diff] [blame] | 1344 | server = tcon->ses->server; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1345 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1346 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1348 | /* Unlink can be called from rename so we can not take the |
| 1349 | * sb->s_vfs_rename_mutex here */ |
| 1350 | full_path = build_path_from_dentry(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | if (full_path == NULL) { |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 1352 | rc = -ENOMEM; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1353 | goto unlink_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | } |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 1355 | |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 1356 | if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
| 1357 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1358 | rc = CIFSPOSIXDelFile(xid, tcon, full_path, |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 1359 | SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls, |
Steve French | 2baa268 | 2014-09-27 02:19:01 -0500 | [diff] [blame] | 1360 | cifs_remap(cifs_sb)); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1361 | cifs_dbg(FYI, "posix del rc %d\n", rc); |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 1362 | if ((rc == 0) || (rc == -ENOENT)) |
| 1363 | goto psx_del_no_retry; |
| 1364 | } |
| 1365 | |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 1366 | retry_std_delete: |
Pavel Shilovsky | ed6875e | 2012-09-18 16:20:25 -0700 | [diff] [blame] | 1367 | if (!server->ops->unlink) { |
| 1368 | rc = -ENOSYS; |
| 1369 | goto psx_del_no_retry; |
| 1370 | } |
| 1371 | |
| 1372 | rc = server->ops->unlink(xid, tcon, full_path, cifs_sb); |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 1373 | |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 1374 | psx_del_no_retry: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | if (!rc) { |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1376 | if (inode) |
Steve French | b7ca692 | 2012-08-03 08:43:01 -0500 | [diff] [blame] | 1377 | cifs_drop_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | } else if (rc == -ENOENT) { |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1379 | d_drop(dentry); |
Sachin Prabhu | 47c78f4 | 2013-03-11 13:08:49 +0000 | [diff] [blame] | 1380 | } else if (rc == -EBUSY) { |
Pavel Shilovsky | ed6875e | 2012-09-18 16:20:25 -0700 | [diff] [blame] | 1381 | if (server->ops->rename_pending_delete) { |
| 1382 | rc = server->ops->rename_pending_delete(full_path, |
| 1383 | dentry, xid); |
| 1384 | if (rc == 0) |
| 1385 | cifs_drop_nlink(inode); |
| 1386 | } |
Steve French | ff69452 | 2009-04-20 19:45:13 +0000 | [diff] [blame] | 1387 | } else if ((rc == -EACCES) && (dosattr == 0) && inode) { |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 1388 | attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); |
| 1389 | if (attrs == NULL) { |
| 1390 | rc = -ENOMEM; |
| 1391 | goto out_reval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | } |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 1393 | |
| 1394 | /* try to reset dos attributes */ |
Steve French | ff69452 | 2009-04-20 19:45:13 +0000 | [diff] [blame] | 1395 | cifs_inode = CIFS_I(inode); |
| 1396 | origattr = cifs_inode->cifsAttrs; |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 1397 | if (origattr == 0) |
| 1398 | origattr |= ATTR_NORMAL; |
| 1399 | dosattr = origattr & ~ATTR_READONLY; |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 1400 | if (dosattr == 0) |
| 1401 | dosattr |= ATTR_NORMAL; |
| 1402 | dosattr |= ATTR_HIDDEN; |
| 1403 | |
| 1404 | rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr); |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 1405 | if (rc != 0) |
| 1406 | goto out_reval; |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 1407 | |
| 1408 | goto retry_std_delete; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | } |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 1410 | |
| 1411 | /* undo the setattr if we errored out and it's needed */ |
| 1412 | if (rc != 0 && dosattr != 0) |
| 1413 | cifs_set_file_info(inode, attrs, xid, full_path, origattr); |
| 1414 | |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 1415 | out_reval: |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1416 | if (inode) { |
Steve French | ff69452 | 2009-04-20 19:45:13 +0000 | [diff] [blame] | 1417 | cifs_inode = CIFS_I(inode); |
| 1418 | cifs_inode->time = 0; /* will force revalidate to get info |
Steve French | b2aeb9d | 2005-05-17 13:16:18 -0500 | [diff] [blame] | 1419 | when needed */ |
Deepa Dinamani | e37fea5 | 2017-05-08 15:59:16 -0700 | [diff] [blame] | 1420 | inode->i_ctime = current_time(inode); |
Steve French | b2aeb9d | 2005-05-17 13:16:18 -0500 | [diff] [blame] | 1421 | } |
Deepa Dinamani | e37fea5 | 2017-05-08 15:59:16 -0700 | [diff] [blame] | 1422 | dir->i_ctime = dir->i_mtime = current_time(dir); |
Steve French | ff69452 | 2009-04-20 19:45:13 +0000 | [diff] [blame] | 1423 | cifs_inode = CIFS_I(dir); |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 1424 | CIFS_I(dir)->time = 0; /* force revalidate of dir as well */ |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1425 | unlink_out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | kfree(full_path); |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 1427 | kfree(attrs); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1428 | free_xid(xid); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1429 | cifs_put_tlink(tlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | return rc; |
| 1431 | } |
| 1432 | |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1433 | static int |
Jeff Layton | 101b92d | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 1434 | cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode, |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1435 | const char *full_path, struct cifs_sb_info *cifs_sb, |
| 1436 | struct cifs_tcon *tcon, const unsigned int xid) |
| 1437 | { |
| 1438 | int rc = 0; |
Jeff Layton | 101b92d | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 1439 | struct inode *inode = NULL; |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1440 | |
| 1441 | if (tcon->unix_ext) |
Jeff Layton | 101b92d | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 1442 | rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb, |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1443 | xid); |
| 1444 | else |
Jeff Layton | 101b92d | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 1445 | rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb, |
| 1446 | xid, NULL); |
| 1447 | |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1448 | if (rc) |
| 1449 | return rc; |
| 1450 | |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1451 | /* |
| 1452 | * setting nlink not necessary except in cases where we failed to get it |
Jeff Layton | 101b92d | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 1453 | * from the server or was set bogus. Also, since this is a brand new |
| 1454 | * inode, no need to grab the i_lock before setting the i_nlink. |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1455 | */ |
Jeff Layton | 101b92d | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 1456 | if (inode->i_nlink < 2) |
| 1457 | set_nlink(inode, 2); |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1458 | mode &= ~current_umask(); |
| 1459 | /* must turn on setgid bit if parent dir has it */ |
Jeff Layton | 101b92d | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 1460 | if (parent->i_mode & S_ISGID) |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1461 | mode |= S_ISGID; |
| 1462 | |
| 1463 | if (tcon->unix_ext) { |
| 1464 | struct cifs_unix_set_info_args args = { |
| 1465 | .mode = mode, |
| 1466 | .ctime = NO_CHANGE_64, |
| 1467 | .atime = NO_CHANGE_64, |
| 1468 | .mtime = NO_CHANGE_64, |
| 1469 | .device = 0, |
| 1470 | }; |
| 1471 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 1472 | args.uid = current_fsuid(); |
Jeff Layton | 101b92d | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 1473 | if (parent->i_mode & S_ISGID) |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 1474 | args.gid = parent->i_gid; |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1475 | else |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 1476 | args.gid = current_fsgid(); |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1477 | } else { |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 1478 | args.uid = INVALID_UID; /* no change */ |
| 1479 | args.gid = INVALID_GID; /* no change */ |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1480 | } |
| 1481 | CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args, |
| 1482 | cifs_sb->local_nls, |
Steve French | 2baa268 | 2014-09-27 02:19:01 -0500 | [diff] [blame] | 1483 | cifs_remap(cifs_sb)); |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1484 | } else { |
Pavel Shilovsky | f436720 | 2012-03-17 11:41:12 +0300 | [diff] [blame] | 1485 | struct TCP_Server_Info *server = tcon->ses->server; |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1486 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) && |
Pavel Shilovsky | f436720 | 2012-03-17 11:41:12 +0300 | [diff] [blame] | 1487 | (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo) |
Jeff Layton | 101b92d | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 1488 | server->ops->mkdir_setinfo(inode, full_path, cifs_sb, |
Pavel Shilovsky | f436720 | 2012-03-17 11:41:12 +0300 | [diff] [blame] | 1489 | tcon, xid); |
Jeff Layton | 101b92d | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 1490 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) |
| 1491 | inode->i_mode = (mode | S_IFDIR); |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1492 | |
Jeff Layton | 101b92d | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 1493 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { |
| 1494 | inode->i_uid = current_fsuid(); |
| 1495 | if (inode->i_mode & S_ISGID) |
| 1496 | inode->i_gid = parent->i_gid; |
| 1497 | else |
| 1498 | inode->i_gid = current_fsgid(); |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1499 | } |
| 1500 | } |
Jeff Layton | 101b92d | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 1501 | d_instantiate(dentry, inode); |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1502 | return rc; |
| 1503 | } |
| 1504 | |
| 1505 | static int |
| 1506 | cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode, |
| 1507 | const char *full_path, struct cifs_sb_info *cifs_sb, |
| 1508 | struct cifs_tcon *tcon, const unsigned int xid) |
| 1509 | { |
| 1510 | int rc = 0; |
| 1511 | u32 oplock = 0; |
| 1512 | FILE_UNIX_BASIC_INFO *info = NULL; |
| 1513 | struct inode *newinode = NULL; |
| 1514 | struct cifs_fattr fattr; |
| 1515 | |
| 1516 | info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL); |
| 1517 | if (info == NULL) { |
| 1518 | rc = -ENOMEM; |
| 1519 | goto posix_mkdir_out; |
| 1520 | } |
| 1521 | |
| 1522 | mode &= ~current_umask(); |
| 1523 | rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode, |
| 1524 | NULL /* netfid */, info, &oplock, full_path, |
Steve French | 2baa268 | 2014-09-27 02:19:01 -0500 | [diff] [blame] | 1525 | cifs_sb->local_nls, cifs_remap(cifs_sb)); |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1526 | if (rc == -EOPNOTSUPP) |
| 1527 | goto posix_mkdir_out; |
| 1528 | else if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1529 | cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc); |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1530 | d_drop(dentry); |
| 1531 | goto posix_mkdir_out; |
| 1532 | } |
| 1533 | |
| 1534 | if (info->Type == cpu_to_le32(-1)) |
| 1535 | /* no return info, go query for it */ |
| 1536 | goto posix_mkdir_get_info; |
| 1537 | /* |
| 1538 | * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if |
| 1539 | * need to set uid/gid. |
| 1540 | */ |
| 1541 | |
| 1542 | cifs_unix_basic_to_fattr(&fattr, info, cifs_sb); |
| 1543 | cifs_fill_uniqueid(inode->i_sb, &fattr); |
| 1544 | newinode = cifs_iget(inode->i_sb, &fattr); |
| 1545 | if (!newinode) |
| 1546 | goto posix_mkdir_get_info; |
| 1547 | |
| 1548 | d_instantiate(dentry, newinode); |
| 1549 | |
| 1550 | #ifdef CONFIG_CIFS_DEBUG2 |
Al Viro | 35c265e | 2014-08-19 20:25:34 -0400 | [diff] [blame] | 1551 | cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n", |
| 1552 | dentry, dentry, newinode); |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1553 | |
| 1554 | if (newinode->i_nlink != 2) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1555 | cifs_dbg(FYI, "unexpected number of links %d\n", |
| 1556 | newinode->i_nlink); |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1557 | #endif |
| 1558 | |
| 1559 | posix_mkdir_out: |
| 1560 | kfree(info); |
| 1561 | return rc; |
| 1562 | posix_mkdir_get_info: |
| 1563 | rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon, |
| 1564 | xid); |
| 1565 | goto posix_mkdir_out; |
| 1566 | } |
| 1567 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 1568 | int cifs_mkdir(struct inode *inode, struct dentry *direntry, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | { |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1570 | int rc = 0; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1571 | unsigned int xid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | struct cifs_sb_info *cifs_sb; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1573 | struct tcon_link *tlink; |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 1574 | struct cifs_tcon *tcon; |
Pavel Shilovsky | f436720 | 2012-03-17 11:41:12 +0300 | [diff] [blame] | 1575 | struct TCP_Server_Info *server; |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1576 | char *full_path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1578 | cifs_dbg(FYI, "In cifs_mkdir, mode = 0x%hx inode = 0x%p\n", |
| 1579 | mode, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | cifs_sb = CIFS_SB(inode->i_sb); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1582 | tlink = cifs_sb_tlink(cifs_sb); |
| 1583 | if (IS_ERR(tlink)) |
| 1584 | return PTR_ERR(tlink); |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 1585 | tcon = tlink_tcon(tlink); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1586 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1587 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | |
Steve French | 7f57356 | 2005-08-30 11:32:14 -0700 | [diff] [blame] | 1589 | full_path = build_path_from_dentry(direntry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | if (full_path == NULL) { |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 1591 | rc = -ENOMEM; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1592 | goto mkdir_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1594 | |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 1595 | server = tcon->ses->server; |
| 1596 | |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 1597 | if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) { |
| 1598 | rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path, |
| 1599 | cifs_sb); |
| 1600 | d_drop(direntry); /* for time being always refresh inode info */ |
| 1601 | goto mkdir_out; |
| 1602 | } |
Steve French | bea851b | 2018-06-14 21:56:32 -0500 | [diff] [blame] | 1603 | |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 1604 | if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
| 1605 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1606 | rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb, |
| 1607 | tcon, xid); |
| 1608 | if (rc != -EOPNOTSUPP) |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1609 | goto mkdir_out; |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1610 | } |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1611 | |
Pavel Shilovsky | f436720 | 2012-03-17 11:41:12 +0300 | [diff] [blame] | 1612 | if (!server->ops->mkdir) { |
| 1613 | rc = -ENOSYS; |
| 1614 | goto mkdir_out; |
| 1615 | } |
| 1616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | /* BB add setting the equivalent of mode via CreateX w/ACLs */ |
Pavel Shilovsky | f436720 | 2012-03-17 11:41:12 +0300 | [diff] [blame] | 1618 | rc = server->ops->mkdir(xid, tcon, full_path, cifs_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1620 | cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | d_drop(direntry); |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1622 | goto mkdir_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | } |
Pavel Shilovsky | ff691e9 | 2012-07-13 14:04:46 +0400 | [diff] [blame] | 1624 | |
| 1625 | rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon, |
| 1626 | xid); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1627 | mkdir_out: |
Pavel Shilovsky | 6de2ce4 | 2012-02-17 16:13:30 +0300 | [diff] [blame] | 1628 | /* |
| 1629 | * Force revalidate to get parent dir info when needed since cached |
| 1630 | * attributes are invalid now. |
| 1631 | */ |
| 1632 | CIFS_I(inode)->time = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | kfree(full_path); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1634 | free_xid(xid); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1635 | cifs_put_tlink(tlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | return rc; |
| 1637 | } |
| 1638 | |
| 1639 | int cifs_rmdir(struct inode *inode, struct dentry *direntry) |
| 1640 | { |
| 1641 | int rc = 0; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1642 | unsigned int xid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | struct cifs_sb_info *cifs_sb; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1644 | struct tcon_link *tlink; |
Pavel Shilovsky | f958ca5 | 2012-07-10 16:14:18 +0400 | [diff] [blame] | 1645 | struct cifs_tcon *tcon; |
| 1646 | struct TCP_Server_Info *server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | char *full_path = NULL; |
| 1648 | struct cifsInodeInfo *cifsInode; |
| 1649 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1650 | cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1652 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | |
Steve French | 7f57356 | 2005-08-30 11:32:14 -0700 | [diff] [blame] | 1654 | full_path = build_path_from_dentry(direntry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | if (full_path == NULL) { |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 1656 | rc = -ENOMEM; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1657 | goto rmdir_exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | } |
| 1659 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1660 | cifs_sb = CIFS_SB(inode->i_sb); |
| 1661 | tlink = cifs_sb_tlink(cifs_sb); |
| 1662 | if (IS_ERR(tlink)) { |
| 1663 | rc = PTR_ERR(tlink); |
| 1664 | goto rmdir_exit; |
| 1665 | } |
Pavel Shilovsky | f958ca5 | 2012-07-10 16:14:18 +0400 | [diff] [blame] | 1666 | tcon = tlink_tcon(tlink); |
| 1667 | server = tcon->ses->server; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1668 | |
Pavel Shilovsky | f958ca5 | 2012-07-10 16:14:18 +0400 | [diff] [blame] | 1669 | if (!server->ops->rmdir) { |
| 1670 | rc = -ENOSYS; |
| 1671 | cifs_put_tlink(tlink); |
| 1672 | goto rmdir_exit; |
| 1673 | } |
| 1674 | |
| 1675 | rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1676 | cifs_put_tlink(tlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | |
| 1678 | if (!rc) { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1679 | spin_lock(&d_inode(direntry)->i_lock); |
| 1680 | i_size_write(d_inode(direntry), 0); |
| 1681 | clear_nlink(d_inode(direntry)); |
| 1682 | spin_unlock(&d_inode(direntry)->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | } |
| 1684 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1685 | cifsInode = CIFS_I(d_inode(direntry)); |
Pavel Shilovsky | 6de2ce4 | 2012-02-17 16:13:30 +0300 | [diff] [blame] | 1686 | /* force revalidate to go get info when needed */ |
| 1687 | cifsInode->time = 0; |
Steve French | 42c24544 | 2009-01-13 22:03:55 +0000 | [diff] [blame] | 1688 | |
| 1689 | cifsInode = CIFS_I(inode); |
Pavel Shilovsky | 6de2ce4 | 2012-02-17 16:13:30 +0300 | [diff] [blame] | 1690 | /* |
| 1691 | * Force revalidate to get parent dir info when needed since cached |
| 1692 | * attributes are invalid now. |
| 1693 | */ |
| 1694 | cifsInode->time = 0; |
Steve French | 42c24544 | 2009-01-13 22:03:55 +0000 | [diff] [blame] | 1695 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1696 | d_inode(direntry)->i_ctime = inode->i_ctime = inode->i_mtime = |
Deepa Dinamani | e37fea5 | 2017-05-08 15:59:16 -0700 | [diff] [blame] | 1697 | current_time(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1699 | rmdir_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | kfree(full_path); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1701 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | return rc; |
| 1703 | } |
| 1704 | |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1705 | static int |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1706 | cifs_do_rename(const unsigned int xid, struct dentry *from_dentry, |
| 1707 | const char *from_path, struct dentry *to_dentry, |
| 1708 | const char *to_path) |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1709 | { |
| 1710 | struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1711 | struct tcon_link *tlink; |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1712 | struct cifs_tcon *tcon; |
| 1713 | struct TCP_Server_Info *server; |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 1714 | struct cifs_fid fid; |
| 1715 | struct cifs_open_parms oparms; |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1716 | int oplock, rc; |
| 1717 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1718 | tlink = cifs_sb_tlink(cifs_sb); |
| 1719 | if (IS_ERR(tlink)) |
| 1720 | return PTR_ERR(tlink); |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1721 | tcon = tlink_tcon(tlink); |
| 1722 | server = tcon->ses->server; |
| 1723 | |
| 1724 | if (!server->ops->rename) |
| 1725 | return -ENOSYS; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1726 | |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1727 | /* try path-based rename first */ |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1728 | rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb); |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1729 | |
| 1730 | /* |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1731 | * Don't bother with rename by filehandle unless file is busy and |
| 1732 | * source. Note that cross directory moves do not work with |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1733 | * rename by filehandle to various Windows servers. |
| 1734 | */ |
Sachin Prabhu | 47c78f4 | 2013-03-11 13:08:49 +0000 | [diff] [blame] | 1735 | if (rc == 0 || rc != -EBUSY) |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1736 | goto do_rename_exit; |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1737 | |
Jeff Layton | ed0e3ac | 2010-06-01 16:21:01 -0400 | [diff] [blame] | 1738 | /* open-file renames don't work across directories */ |
| 1739 | if (to_dentry->d_parent != from_dentry->d_parent) |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1740 | goto do_rename_exit; |
Jeff Layton | ed0e3ac | 2010-06-01 16:21:01 -0400 | [diff] [blame] | 1741 | |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 1742 | oparms.tcon = tcon; |
| 1743 | oparms.cifs_sb = cifs_sb; |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1744 | /* open the file to be renamed -- we need DELETE perms */ |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 1745 | oparms.desired_access = DELETE; |
| 1746 | oparms.create_options = CREATE_NOT_DIR; |
| 1747 | oparms.disposition = FILE_OPEN; |
| 1748 | oparms.path = from_path; |
| 1749 | oparms.fid = &fid; |
| 1750 | oparms.reconnect = false; |
| 1751 | |
| 1752 | rc = CIFS_open(xid, &oparms, &oplock, NULL); |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1753 | if (rc == 0) { |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 1754 | rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1755 | (const char *) to_dentry->d_name.name, |
Steve French | 2baa268 | 2014-09-27 02:19:01 -0500 | [diff] [blame] | 1756 | cifs_sb->local_nls, cifs_remap(cifs_sb)); |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 1757 | CIFSSMBClose(xid, tcon, fid.netfid); |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1758 | } |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1759 | do_rename_exit: |
| 1760 | cifs_put_tlink(tlink); |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1761 | return rc; |
| 1762 | } |
| 1763 | |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1764 | int |
Miklos Szeredi | 7c33d59 | 2014-07-23 15:15:36 +0200 | [diff] [blame] | 1765 | cifs_rename2(struct inode *source_dir, struct dentry *source_dentry, |
| 1766 | struct inode *target_dir, struct dentry *target_dentry, |
| 1767 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | { |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1769 | char *from_name = NULL; |
| 1770 | char *to_name = NULL; |
Jeff Layton | 639e7a9 | 2010-09-03 11:50:09 -0400 | [diff] [blame] | 1771 | struct cifs_sb_info *cifs_sb; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1772 | struct tcon_link *tlink; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1773 | struct cifs_tcon *tcon; |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1774 | FILE_UNIX_BASIC_INFO *info_buf_source = NULL; |
| 1775 | FILE_UNIX_BASIC_INFO *info_buf_target; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1776 | unsigned int xid; |
| 1777 | int rc, tmprc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | |
Miklos Szeredi | 7c33d59 | 2014-07-23 15:15:36 +0200 | [diff] [blame] | 1779 | if (flags & ~RENAME_NOREPLACE) |
| 1780 | return -EINVAL; |
| 1781 | |
Jeff Layton | 639e7a9 | 2010-09-03 11:50:09 -0400 | [diff] [blame] | 1782 | cifs_sb = CIFS_SB(source_dir->i_sb); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1783 | tlink = cifs_sb_tlink(cifs_sb); |
| 1784 | if (IS_ERR(tlink)) |
| 1785 | return PTR_ERR(tlink); |
| 1786 | tcon = tlink_tcon(tlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1788 | xid = get_xid(); |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1789 | |
| 1790 | /* |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1791 | * we already have the rename sem so we do not need to |
| 1792 | * grab it again here to protect the path integrity |
| 1793 | */ |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1794 | from_name = build_path_from_dentry(source_dentry); |
| 1795 | if (from_name == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | rc = -ENOMEM; |
| 1797 | goto cifs_rename_exit; |
| 1798 | } |
| 1799 | |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1800 | to_name = build_path_from_dentry(target_dentry); |
| 1801 | if (to_name == NULL) { |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1802 | rc = -ENOMEM; |
| 1803 | goto cifs_rename_exit; |
| 1804 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1806 | rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry, |
| 1807 | to_name); |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1808 | |
Miklos Szeredi | 7c33d59 | 2014-07-23 15:15:36 +0200 | [diff] [blame] | 1809 | /* |
| 1810 | * No-replace is the natural behavior for CIFS, so skip unlink hacks. |
| 1811 | */ |
| 1812 | if (flags & RENAME_NOREPLACE) |
| 1813 | goto cifs_rename_exit; |
| 1814 | |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1815 | if (rc == -EEXIST && tcon->unix_ext) { |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1816 | /* |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1817 | * Are src and dst hardlinks of same inode? We can only tell |
| 1818 | * with unix extensions enabled. |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1819 | */ |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1820 | info_buf_source = |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 1821 | kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO), |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1822 | GFP_KERNEL); |
| 1823 | if (info_buf_source == NULL) { |
| 1824 | rc = -ENOMEM; |
| 1825 | goto cifs_rename_exit; |
| 1826 | } |
| 1827 | |
| 1828 | info_buf_target = info_buf_source + 1; |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1829 | tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name, |
| 1830 | info_buf_source, |
| 1831 | cifs_sb->local_nls, |
Steve French | 2baa268 | 2014-09-27 02:19:01 -0500 | [diff] [blame] | 1832 | cifs_remap(cifs_sb)); |
Jeff Layton | 8d281ef | 2008-10-22 13:57:01 -0400 | [diff] [blame] | 1833 | if (tmprc != 0) |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1834 | goto unlink_target; |
| 1835 | |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1836 | tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name, |
| 1837 | info_buf_target, |
| 1838 | cifs_sb->local_nls, |
Steve French | 2baa268 | 2014-09-27 02:19:01 -0500 | [diff] [blame] | 1839 | cifs_remap(cifs_sb)); |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1840 | |
Jeff Layton | 8d281ef | 2008-10-22 13:57:01 -0400 | [diff] [blame] | 1841 | if (tmprc == 0 && (info_buf_source->UniqueId == |
Jeff Layton | ae6884a | 2008-11-03 14:05:08 -0500 | [diff] [blame] | 1842 | info_buf_target->UniqueId)) { |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1843 | /* same file, POSIX says that this is a noop */ |
Jeff Layton | ae6884a | 2008-11-03 14:05:08 -0500 | [diff] [blame] | 1844 | rc = 0; |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1845 | goto cifs_rename_exit; |
Jeff Layton | ae6884a | 2008-11-03 14:05:08 -0500 | [diff] [blame] | 1846 | } |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1847 | } |
| 1848 | /* |
| 1849 | * else ... BB we could add the same check for Windows by |
| 1850 | * checking the UniqueId via FILE_INTERNAL_INFO |
| 1851 | */ |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1852 | |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1853 | unlink_target: |
Jeff Layton | fc6f394 | 2009-04-17 11:45:30 -0400 | [diff] [blame] | 1854 | /* Try unlinking the target dentry if it's not negative */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1855 | if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) { |
Pavel Shilovsky | a07d322 | 2014-08-22 13:32:09 +0400 | [diff] [blame] | 1856 | if (d_is_dir(target_dentry)) |
| 1857 | tmprc = cifs_rmdir(target_dir, target_dentry); |
| 1858 | else |
| 1859 | tmprc = cifs_unlink(target_dir, target_dentry); |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1860 | if (tmprc) |
| 1861 | goto cifs_rename_exit; |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1862 | rc = cifs_do_rename(xid, source_dentry, from_name, |
| 1863 | target_dentry, to_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | } |
| 1865 | |
Pavel Shilovsky | b46799a | 2014-08-18 20:49:58 +0400 | [diff] [blame] | 1866 | /* force revalidate to go get info when needed */ |
| 1867 | CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0; |
| 1868 | |
| 1869 | source_dir->i_ctime = source_dir->i_mtime = target_dir->i_ctime = |
Deepa Dinamani | e37fea5 | 2017-05-08 15:59:16 -0700 | [diff] [blame] | 1870 | target_dir->i_mtime = current_time(source_dir); |
Pavel Shilovsky | b46799a | 2014-08-18 20:49:58 +0400 | [diff] [blame] | 1871 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | cifs_rename_exit: |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1873 | kfree(info_buf_source); |
Pavel Shilovsky | 8ceb984 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1874 | kfree(from_name); |
| 1875 | kfree(to_name); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1876 | free_xid(xid); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1877 | cifs_put_tlink(tlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | return rc; |
| 1879 | } |
| 1880 | |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 1881 | static bool |
| 1882 | cifs_inode_needs_reval(struct inode *inode) |
| 1883 | { |
| 1884 | struct cifsInodeInfo *cifs_i = CIFS_I(inode); |
Suresh Jayaraman | 6d20e84 | 2010-12-01 14:42:28 +0530 | [diff] [blame] | 1885 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 1886 | |
Ronnie Sahlberg | 57c55cd | 2018-05-24 06:54:27 +1000 | [diff] [blame] | 1887 | if (cifs_i->time == 0) |
| 1888 | return true; |
| 1889 | |
Pavel Shilovsky | 18cceb6 | 2013-09-05 13:01:06 +0400 | [diff] [blame] | 1890 | if (CIFS_CACHE_READ(cifs_i)) |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 1891 | return false; |
| 1892 | |
| 1893 | if (!lookupCacheEnabled) |
| 1894 | return true; |
| 1895 | |
Jeff Layton | a87c9ad | 2014-03-26 07:24:23 -0700 | [diff] [blame] | 1896 | if (!cifs_sb->actimeo) |
| 1897 | return true; |
| 1898 | |
Suresh Jayaraman | 6d20e84 | 2010-12-01 14:42:28 +0530 | [diff] [blame] | 1899 | if (!time_in_range(jiffies, cifs_i->time, |
| 1900 | cifs_i->time + cifs_sb->actimeo)) |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 1901 | return true; |
| 1902 | |
Jeff Layton | db19272 | 2010-05-17 14:51:49 -0400 | [diff] [blame] | 1903 | /* hardlinked files w/ noserverino get "special" treatment */ |
Suresh Jayaraman | 6d20e84 | 2010-12-01 14:42:28 +0530 | [diff] [blame] | 1904 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) && |
Jeff Layton | db19272 | 2010-05-17 14:51:49 -0400 | [diff] [blame] | 1905 | S_ISREG(inode->i_mode) && inode->i_nlink != 1) |
| 1906 | return true; |
| 1907 | |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 1908 | return false; |
| 1909 | } |
| 1910 | |
Suresh Jayaraman | 523fb8c | 2010-11-29 22:39:47 +0530 | [diff] [blame] | 1911 | /* |
| 1912 | * Zap the cache. Called when invalid_mapping flag is set. |
| 1913 | */ |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 1914 | int |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 1915 | cifs_invalidate_mapping(struct inode *inode) |
| 1916 | { |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 1917 | int rc = 0; |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 1918 | |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 1919 | if (inode->i_mapping && inode->i_mapping->nrpages != 0) { |
Pavel Shilovsky | 257fb1f | 2011-03-16 01:55:32 +0300 | [diff] [blame] | 1920 | rc = invalidate_inode_pages2(inode->i_mapping); |
Jeff Layton | 4f73c7d | 2014-04-30 09:31:47 -0400 | [diff] [blame] | 1921 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1922 | cifs_dbg(VFS, "%s: could not invalidate inode %p\n", |
| 1923 | __func__, inode); |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 1924 | } |
Pavel Shilovsky | 257fb1f | 2011-03-16 01:55:32 +0300 | [diff] [blame] | 1925 | |
Suresh Jayaraman | 9451a9a | 2010-07-05 18:12:45 +0530 | [diff] [blame] | 1926 | cifs_fscache_reset_inode_cookie(inode); |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 1927 | return rc; |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 1928 | } |
| 1929 | |
Jeff Layton | 4f73c7d | 2014-04-30 09:31:47 -0400 | [diff] [blame] | 1930 | /** |
| 1931 | * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks |
| 1932 | * @word: long word containing the bit lock |
| 1933 | */ |
| 1934 | static int |
Peter Zijlstra | dfd01f0 | 2015-12-13 22:11:16 +0100 | [diff] [blame] | 1935 | cifs_wait_bit_killable(struct wait_bit_key *key, int mode) |
Jeff Layton | 4f73c7d | 2014-04-30 09:31:47 -0400 | [diff] [blame] | 1936 | { |
Jeff Layton | 4f73c7d | 2014-04-30 09:31:47 -0400 | [diff] [blame] | 1937 | freezable_schedule_unsafe(); |
Peter Zijlstra | dfd01f0 | 2015-12-13 22:11:16 +0100 | [diff] [blame] | 1938 | if (signal_pending_state(mode, current)) |
| 1939 | return -ERESTARTSYS; |
Jeff Layton | 4f73c7d | 2014-04-30 09:31:47 -0400 | [diff] [blame] | 1940 | return 0; |
| 1941 | } |
| 1942 | |
Jeff Layton | e284e53 | 2014-04-30 09:31:46 -0400 | [diff] [blame] | 1943 | int |
| 1944 | cifs_revalidate_mapping(struct inode *inode) |
| 1945 | { |
Jeff Layton | 4f73c7d | 2014-04-30 09:31:47 -0400 | [diff] [blame] | 1946 | int rc; |
| 1947 | unsigned long *flags = &CIFS_I(inode)->flags; |
| 1948 | |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 1949 | rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable, |
| 1950 | TASK_KILLABLE); |
Jeff Layton | 4f73c7d | 2014-04-30 09:31:47 -0400 | [diff] [blame] | 1951 | if (rc) |
| 1952 | return rc; |
| 1953 | |
| 1954 | if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) { |
| 1955 | rc = cifs_invalidate_mapping(inode); |
| 1956 | if (rc) |
| 1957 | set_bit(CIFS_INO_INVALID_MAPPING, flags); |
| 1958 | } |
| 1959 | |
| 1960 | clear_bit_unlock(CIFS_INO_LOCK, flags); |
Linus Torvalds | b1cce80 | 2014-06-09 19:08:43 -0700 | [diff] [blame] | 1961 | smp_mb__after_atomic(); |
Jeff Layton | 4f73c7d | 2014-04-30 09:31:47 -0400 | [diff] [blame] | 1962 | wake_up_bit(flags, CIFS_INO_LOCK); |
| 1963 | |
| 1964 | return rc; |
| 1965 | } |
| 1966 | |
| 1967 | int |
| 1968 | cifs_zap_mapping(struct inode *inode) |
| 1969 | { |
| 1970 | set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags); |
| 1971 | return cifs_revalidate_mapping(inode); |
Jeff Layton | e284e53 | 2014-04-30 09:31:46 -0400 | [diff] [blame] | 1972 | } |
| 1973 | |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 1974 | int cifs_revalidate_file_attr(struct file *filp) |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 1975 | { |
| 1976 | int rc = 0; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1977 | struct inode *inode = file_inode(filp); |
Jeff Layton | ba00ba64 | 2010-09-20 16:01:31 -0700 | [diff] [blame] | 1978 | struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data; |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 1979 | |
| 1980 | if (!cifs_inode_needs_reval(inode)) |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 1981 | return rc; |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 1982 | |
Jeff Layton | 13cfb73 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1983 | if (tlink_tcon(cfile->tlink)->unix_ext) |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 1984 | rc = cifs_get_file_info_unix(filp); |
| 1985 | else |
| 1986 | rc = cifs_get_file_info(filp); |
| 1987 | |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 1988 | return rc; |
| 1989 | } |
| 1990 | |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 1991 | int cifs_revalidate_dentry_attr(struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1993 | unsigned int xid; |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 1994 | int rc = 0; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1995 | struct inode *inode = d_inode(dentry); |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 1996 | struct super_block *sb = dentry->d_sb; |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 1997 | char *full_path = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 1999 | if (inode == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | return -ENOENT; |
| 2001 | |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 2002 | if (!cifs_inode_needs_reval(inode)) |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 2003 | return rc; |
| 2004 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2005 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | |
| 2007 | /* can not safely grab the rename sem here if rename calls revalidate |
| 2008 | since that would deadlock */ |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 2009 | full_path = build_path_from_dentry(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | if (full_path == NULL) { |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 2011 | rc = -ENOMEM; |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 2012 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | } |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 2014 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2015 | cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n", |
| 2016 | full_path, inode, inode->i_count.counter, |
Miklos Szeredi | a00be0e | 2016-09-16 12:44:21 +0200 | [diff] [blame] | 2017 | dentry, cifs_get_time(dentry), jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | |
Jeff Layton | 0d424ad | 2010-09-20 16:01:35 -0700 | [diff] [blame] | 2019 | if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext) |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 2020 | rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid); |
| 2021 | else |
| 2022 | rc = cifs_get_inode_info(&inode, full_path, NULL, sb, |
| 2023 | xid, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 2025 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | kfree(full_path); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2027 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | return rc; |
| 2029 | } |
| 2030 | |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 2031 | int cifs_revalidate_file(struct file *filp) |
| 2032 | { |
| 2033 | int rc; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2034 | struct inode *inode = file_inode(filp); |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 2035 | |
| 2036 | rc = cifs_revalidate_file_attr(filp); |
| 2037 | if (rc) |
| 2038 | return rc; |
| 2039 | |
Jeff Layton | e284e53 | 2014-04-30 09:31:46 -0400 | [diff] [blame] | 2040 | return cifs_revalidate_mapping(inode); |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | /* revalidate a dentry's inode attributes */ |
| 2044 | int cifs_revalidate_dentry(struct dentry *dentry) |
| 2045 | { |
| 2046 | int rc; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2047 | struct inode *inode = d_inode(dentry); |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 2048 | |
| 2049 | rc = cifs_revalidate_dentry_attr(dentry); |
| 2050 | if (rc) |
| 2051 | return rc; |
| 2052 | |
Jeff Layton | e284e53 | 2014-04-30 09:31:46 -0400 | [diff] [blame] | 2053 | return cifs_revalidate_mapping(inode); |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 2054 | } |
| 2055 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 2056 | int cifs_getattr(const struct path *path, struct kstat *stat, |
| 2057 | u32 request_mask, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | { |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 2059 | struct dentry *dentry = path->dentry; |
Jeff Layton | 3aa1c8c | 2010-10-07 14:46:28 -0400 | [diff] [blame] | 2060 | struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb); |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2061 | struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2062 | struct inode *inode = d_inode(dentry); |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 2063 | int rc; |
Jeff Layton | 3aa1c8c | 2010-10-07 14:46:28 -0400 | [diff] [blame] | 2064 | |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 2065 | /* |
| 2066 | * We need to be sure that all dirty pages are written and the server |
| 2067 | * has actual ctime, mtime and file length. |
| 2068 | */ |
Pavel Shilovsky | 18cceb6 | 2013-09-05 13:01:06 +0400 | [diff] [blame] | 2069 | if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping && |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 2070 | inode->i_mapping->nrpages != 0) { |
| 2071 | rc = filemap_fdatawait(inode->i_mapping); |
Steve French | 156ecb2 | 2011-05-20 17:00:01 +0000 | [diff] [blame] | 2072 | if (rc) { |
| 2073 | mapping_set_error(inode->i_mapping, rc); |
| 2074 | return rc; |
| 2075 | } |
Steve French | 5fe14c8 | 2006-11-07 19:26:33 +0000 | [diff] [blame] | 2076 | } |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 2077 | |
| 2078 | rc = cifs_revalidate_dentry_attr(dentry); |
| 2079 | if (rc) |
| 2080 | return rc; |
| 2081 | |
| 2082 | generic_fillattr(inode, stat); |
| 2083 | stat->blksize = CIFS_MAX_MSGSIZE; |
| 2084 | stat->ino = CIFS_I(inode)->uniqueid; |
| 2085 | |
Steve French | 6e70e26 | 2017-09-21 21:32:29 -0500 | [diff] [blame] | 2086 | /* old CIFS Unix Extensions doesn't return create time */ |
| 2087 | if (CIFS_I(inode)->createtime) { |
| 2088 | stat->result_mask |= STATX_BTIME; |
Arnd Bergmann | 9539020 | 2018-06-19 17:27:58 +0200 | [diff] [blame] | 2089 | stat->btime = |
| 2090 | cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime)); |
Steve French | 6e70e26 | 2017-09-21 21:32:29 -0500 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED); |
| 2094 | if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED) |
| 2095 | stat->attributes |= STATX_ATTR_COMPRESSED; |
| 2096 | if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED) |
| 2097 | stat->attributes |= STATX_ATTR_ENCRYPTED; |
| 2098 | |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 2099 | /* |
Jeff Layton | d3d1fce | 2012-11-25 08:00:40 -0500 | [diff] [blame] | 2100 | * If on a multiuser mount without unix extensions or cifsacl being |
| 2101 | * enabled, and the admin hasn't overridden them, set the ownership |
| 2102 | * to the fsuid/fsgid of the current process. |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 2103 | */ |
| 2104 | if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) && |
Jeff Layton | d3d1fce | 2012-11-25 08:00:40 -0500 | [diff] [blame] | 2105 | !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) && |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 2106 | !tcon->unix_ext) { |
| 2107 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) |
| 2108 | stat->uid = current_fsuid(); |
| 2109 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) |
| 2110 | stat->gid = current_fsgid(); |
| 2111 | } |
| 2112 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | static int cifs_truncate_page(struct address_space *mapping, loff_t from) |
| 2116 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2117 | pgoff_t index = from >> PAGE_SHIFT; |
| 2118 | unsigned offset = from & (PAGE_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | int rc = 0; |
| 2121 | |
| 2122 | page = grab_cache_page(mapping, index); |
| 2123 | if (!page) |
| 2124 | return -ENOMEM; |
| 2125 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2126 | zero_user_segment(page, offset, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2128 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | return rc; |
| 2130 | } |
| 2131 | |
Christoph Hellwig | 1b94746 | 2010-07-18 17:51:21 -0400 | [diff] [blame] | 2132 | static void cifs_setsize(struct inode *inode, loff_t offset) |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 2133 | { |
Ronnie Sahlberg | 57c55cd | 2018-05-24 06:54:27 +1000 | [diff] [blame] | 2134 | struct cifsInodeInfo *cifs_i = CIFS_I(inode); |
| 2135 | |
Steve French | ba6a46a | 2007-02-26 20:06:29 +0000 | [diff] [blame] | 2136 | spin_lock(&inode->i_lock); |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 2137 | i_size_write(inode, offset); |
Steve French | ba6a46a | 2007-02-26 20:06:29 +0000 | [diff] [blame] | 2138 | spin_unlock(&inode->i_lock); |
Christoph Hellwig | 1b94746 | 2010-07-18 17:51:21 -0400 | [diff] [blame] | 2139 | |
Ronnie Sahlberg | 57c55cd | 2018-05-24 06:54:27 +1000 | [diff] [blame] | 2140 | /* Cached inode must be refreshed on truncate */ |
| 2141 | cifs_i->time = 0; |
Kirill A. Shutemov | 7caef26 | 2013-09-12 15:13:56 -0700 | [diff] [blame] | 2142 | truncate_pagecache(inode, offset); |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
Jeff Layton | 8efdbde | 2008-07-23 21:28:12 +0000 | [diff] [blame] | 2145 | static int |
| 2146 | cifs_set_file_size(struct inode *inode, struct iattr *attrs, |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2147 | unsigned int xid, char *full_path) |
Jeff Layton | 8efdbde | 2008-07-23 21:28:12 +0000 | [diff] [blame] | 2148 | { |
| 2149 | int rc; |
| 2150 | struct cifsFileInfo *open_file; |
| 2151 | struct cifsInodeInfo *cifsInode = CIFS_I(inode); |
| 2152 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 2153 | struct tcon_link *tlink = NULL; |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 2154 | struct cifs_tcon *tcon = NULL; |
| 2155 | struct TCP_Server_Info *server; |
Jeff Layton | 8efdbde | 2008-07-23 21:28:12 +0000 | [diff] [blame] | 2156 | |
| 2157 | /* |
| 2158 | * To avoid spurious oplock breaks from server, in the case of |
| 2159 | * inodes that we already have open, avoid doing path based |
| 2160 | * setting of file size if we can do it by handle. |
| 2161 | * This keeps our caching token (oplock) and avoids timeouts |
| 2162 | * when the local oplock break takes longer to flush |
| 2163 | * writebehind data than the SMB timeout for the SetPathInfo |
| 2164 | * request would allow |
| 2165 | */ |
Jeff Layton | 6508d90 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 2166 | open_file = find_writable_file(cifsInode, true); |
Jeff Layton | 8efdbde | 2008-07-23 21:28:12 +0000 | [diff] [blame] | 2167 | if (open_file) { |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 2168 | tcon = tlink_tcon(open_file->tlink); |
| 2169 | server = tcon->ses->server; |
| 2170 | if (server->ops->set_file_size) |
| 2171 | rc = server->ops->set_file_size(xid, tcon, open_file, |
| 2172 | attrs->ia_size, false); |
| 2173 | else |
| 2174 | rc = -ENOSYS; |
Dave Kleikamp | 6ab409b | 2009-08-31 11:07:12 -0400 | [diff] [blame] | 2175 | cifsFileInfo_put(open_file); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2176 | cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc); |
Jeff Layton | 8efdbde | 2008-07-23 21:28:12 +0000 | [diff] [blame] | 2177 | } else |
| 2178 | rc = -EINVAL; |
| 2179 | |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 2180 | if (!rc) |
| 2181 | goto set_size_out; |
Jeff Layton | ba00ba64 | 2010-09-20 16:01:31 -0700 | [diff] [blame] | 2182 | |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 2183 | if (tcon == NULL) { |
| 2184 | tlink = cifs_sb_tlink(cifs_sb); |
| 2185 | if (IS_ERR(tlink)) |
| 2186 | return PTR_ERR(tlink); |
| 2187 | tcon = tlink_tcon(tlink); |
| 2188 | server = tcon->ses->server; |
Jeff Layton | 8efdbde | 2008-07-23 21:28:12 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 2191 | /* |
| 2192 | * Set file size by pathname rather than by handle either because no |
| 2193 | * valid, writeable file handle for it was found or because there was |
| 2194 | * an error setting it by handle. |
| 2195 | */ |
| 2196 | if (server->ops->set_path_size) |
| 2197 | rc = server->ops->set_path_size(xid, tcon, full_path, |
| 2198 | attrs->ia_size, cifs_sb, false); |
| 2199 | else |
| 2200 | rc = -ENOSYS; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2201 | cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc); |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 2202 | |
Pavel Shilovsky | d143341 | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 2203 | if (tlink) |
| 2204 | cifs_put_tlink(tlink); |
| 2205 | |
| 2206 | set_size_out: |
Jeff Layton | 8efdbde | 2008-07-23 21:28:12 +0000 | [diff] [blame] | 2207 | if (rc == 0) { |
Jeff Layton | fbec9ab | 2009-04-03 13:44:00 -0400 | [diff] [blame] | 2208 | cifsInode->server_eof = attrs->ia_size; |
Christoph Hellwig | 1b94746 | 2010-07-18 17:51:21 -0400 | [diff] [blame] | 2209 | cifs_setsize(inode, attrs->ia_size); |
Jeff Layton | 8efdbde | 2008-07-23 21:28:12 +0000 | [diff] [blame] | 2210 | cifs_truncate_page(inode->i_mapping, inode->i_size); |
| 2211 | } |
| 2212 | |
| 2213 | return rc; |
| 2214 | } |
| 2215 | |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2216 | static int |
| 2217 | cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs) |
| 2218 | { |
| 2219 | int rc; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2220 | unsigned int xid; |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2221 | char *full_path = NULL; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2222 | struct inode *inode = d_inode(direntry); |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2223 | struct cifsInodeInfo *cifsInode = CIFS_I(inode); |
| 2224 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 2225 | struct tcon_link *tlink; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2226 | struct cifs_tcon *pTcon; |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2227 | struct cifs_unix_set_info_args *args = NULL; |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 2228 | struct cifsFileInfo *open_file; |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2229 | |
Al Viro | 35c265e | 2014-08-19 20:25:34 -0400 | [diff] [blame] | 2230 | cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n", |
| 2231 | direntry, attrs->ia_valid); |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2232 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2233 | xid = get_xid(); |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2234 | |
Christoph Hellwig | db78b87 | 2010-06-04 11:30:03 +0200 | [diff] [blame] | 2235 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) |
| 2236 | attrs->ia_valid |= ATTR_FORCE; |
| 2237 | |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 2238 | rc = setattr_prepare(direntry, attrs); |
Christoph Hellwig | db78b87 | 2010-06-04 11:30:03 +0200 | [diff] [blame] | 2239 | if (rc < 0) |
| 2240 | goto out; |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2241 | |
| 2242 | full_path = build_path_from_dentry(direntry); |
| 2243 | if (full_path == NULL) { |
| 2244 | rc = -ENOMEM; |
| 2245 | goto out; |
| 2246 | } |
| 2247 | |
Jeff Layton | 0f4d634 | 2009-03-26 13:35:37 -0400 | [diff] [blame] | 2248 | /* |
| 2249 | * Attempt to flush data before changing attributes. We need to do |
| 2250 | * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the |
| 2251 | * ownership or mode then we may also need to do this. Here, we take |
| 2252 | * the safe way out and just do the flush on all setattr requests. If |
| 2253 | * the flush returns error, store it to report later and continue. |
| 2254 | * |
| 2255 | * BB: This should be smarter. Why bother flushing pages that |
| 2256 | * will be truncated anyway? Also, should we error out here if |
| 2257 | * the flush returns error? |
| 2258 | */ |
| 2259 | rc = filemap_write_and_wait(inode->i_mapping); |
Pavel Shilovsky | 9a66396 | 2019-01-08 11:15:28 -0800 | [diff] [blame^] | 2260 | if (is_interrupt_error(rc)) { |
| 2261 | rc = -ERESTARTSYS; |
| 2262 | goto out; |
| 2263 | } |
| 2264 | |
Jeff Layton | eb4b756 | 2010-10-22 14:52:29 -0400 | [diff] [blame] | 2265 | mapping_set_error(inode->i_mapping, rc); |
| 2266 | rc = 0; |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2267 | |
| 2268 | if (attrs->ia_valid & ATTR_SIZE) { |
| 2269 | rc = cifs_set_file_size(inode, attrs, xid, full_path); |
| 2270 | if (rc != 0) |
| 2271 | goto out; |
| 2272 | } |
| 2273 | |
| 2274 | /* skip mode change if it's just for clearing setuid/setgid */ |
| 2275 | if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) |
| 2276 | attrs->ia_valid &= ~ATTR_MODE; |
| 2277 | |
| 2278 | args = kmalloc(sizeof(*args), GFP_KERNEL); |
| 2279 | if (args == NULL) { |
| 2280 | rc = -ENOMEM; |
| 2281 | goto out; |
| 2282 | } |
| 2283 | |
| 2284 | /* set up the struct */ |
| 2285 | if (attrs->ia_valid & ATTR_MODE) |
| 2286 | args->mode = attrs->ia_mode; |
| 2287 | else |
| 2288 | args->mode = NO_CHANGE_64; |
| 2289 | |
| 2290 | if (attrs->ia_valid & ATTR_UID) |
| 2291 | args->uid = attrs->ia_uid; |
| 2292 | else |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 2293 | args->uid = INVALID_UID; /* no change */ |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2294 | |
| 2295 | if (attrs->ia_valid & ATTR_GID) |
| 2296 | args->gid = attrs->ia_gid; |
| 2297 | else |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 2298 | args->gid = INVALID_GID; /* no change */ |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2299 | |
| 2300 | if (attrs->ia_valid & ATTR_ATIME) |
Arnd Bergmann | 9539020 | 2018-06-19 17:27:58 +0200 | [diff] [blame] | 2301 | args->atime = cifs_UnixTimeToNT(attrs->ia_atime); |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2302 | else |
| 2303 | args->atime = NO_CHANGE_64; |
| 2304 | |
| 2305 | if (attrs->ia_valid & ATTR_MTIME) |
Arnd Bergmann | 9539020 | 2018-06-19 17:27:58 +0200 | [diff] [blame] | 2306 | args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime); |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2307 | else |
| 2308 | args->mtime = NO_CHANGE_64; |
| 2309 | |
| 2310 | if (attrs->ia_valid & ATTR_CTIME) |
Arnd Bergmann | 9539020 | 2018-06-19 17:27:58 +0200 | [diff] [blame] | 2311 | args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime); |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2312 | else |
| 2313 | args->ctime = NO_CHANGE_64; |
| 2314 | |
| 2315 | args->device = 0; |
Jeff Layton | 6508d90 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 2316 | open_file = find_writable_file(cifsInode, true); |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 2317 | if (open_file) { |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 2318 | u16 nfid = open_file->fid.netfid; |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 2319 | u32 npid = open_file->pid; |
Jeff Layton | 13cfb73 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 2320 | pTcon = tlink_tcon(open_file->tlink); |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 2321 | rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid); |
Dave Kleikamp | 6ab409b | 2009-08-31 11:07:12 -0400 | [diff] [blame] | 2322 | cifsFileInfo_put(open_file); |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 2323 | } else { |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 2324 | tlink = cifs_sb_tlink(cifs_sb); |
| 2325 | if (IS_ERR(tlink)) { |
| 2326 | rc = PTR_ERR(tlink); |
| 2327 | goto out; |
| 2328 | } |
| 2329 | pTcon = tlink_tcon(tlink); |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 2330 | rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args, |
Jeff Layton | 01ea95e | 2009-07-09 20:02:49 -0400 | [diff] [blame] | 2331 | cifs_sb->local_nls, |
Nakajima Akira | bc8ebdc4 | 2015-02-13 15:35:58 +0900 | [diff] [blame] | 2332 | cifs_remap(cifs_sb)); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 2333 | cifs_put_tlink(tlink); |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 2334 | } |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2335 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 2336 | if (rc) |
| 2337 | goto out; |
Steve French | ccd4bb1 | 2010-02-08 17:39:58 +0000 | [diff] [blame] | 2338 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 2339 | if ((attrs->ia_valid & ATTR_SIZE) && |
Christoph Hellwig | 1b94746 | 2010-07-18 17:51:21 -0400 | [diff] [blame] | 2340 | attrs->ia_size != i_size_read(inode)) |
| 2341 | truncate_setsize(inode, attrs->ia_size); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 2342 | |
| 2343 | setattr_copy(inode, attrs); |
| 2344 | mark_inode_dirty(inode); |
| 2345 | |
| 2346 | /* force revalidate when any of these times are set since some |
| 2347 | of the fs types (eg ext3, fat) do not have fine enough |
| 2348 | time granularity to match protocol, and we do not have a |
| 2349 | a way (yet) to query the server fs's time granularity (and |
| 2350 | whether it rounds times down). |
| 2351 | */ |
| 2352 | if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME)) |
| 2353 | cifsInode->time = 0; |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2354 | out: |
| 2355 | kfree(args); |
| 2356 | kfree(full_path); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2357 | free_xid(xid); |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2358 | return rc; |
| 2359 | } |
| 2360 | |
Jeff Layton | 0510eeb | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2361 | static int |
| 2362 | cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2364 | unsigned int xid; |
Eric W. Biederman | 8abf277 | 2013-02-06 00:33:17 -0800 | [diff] [blame] | 2365 | kuid_t uid = INVALID_UID; |
| 2366 | kgid_t gid = INVALID_GID; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2367 | struct inode *inode = d_inode(direntry); |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2368 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2369 | struct cifsInodeInfo *cifsInode = CIFS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | char *full_path = NULL; |
| 2371 | int rc = -EACCES; |
Jeff Layton | feb3e20 | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2372 | __u32 dosattr = 0; |
Jeff Layton | 4e1e7fb | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2373 | __u64 mode = NO_CHANGE_64; |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2374 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2375 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2376 | |
Al Viro | 35c265e | 2014-08-19 20:25:34 -0400 | [diff] [blame] | 2377 | cifs_dbg(FYI, "setattr on file %pd attrs->iavalid 0x%x\n", |
| 2378 | direntry, attrs->ia_valid); |
Steve French | 6473a55 | 2005-11-29 20:20:10 -0800 | [diff] [blame] | 2379 | |
Christoph Hellwig | db78b87 | 2010-06-04 11:30:03 +0200 | [diff] [blame] | 2380 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) |
| 2381 | attrs->ia_valid |= ATTR_FORCE; |
| 2382 | |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 2383 | rc = setattr_prepare(direntry, attrs); |
Christoph Hellwig | db78b87 | 2010-06-04 11:30:03 +0200 | [diff] [blame] | 2384 | if (rc < 0) { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2385 | free_xid(xid); |
Christoph Hellwig | db78b87 | 2010-06-04 11:30:03 +0200 | [diff] [blame] | 2386 | return rc; |
Steve French | 6473a55 | 2005-11-29 20:20:10 -0800 | [diff] [blame] | 2387 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2388 | |
Steve French | 7f57356 | 2005-08-30 11:32:14 -0700 | [diff] [blame] | 2389 | full_path = build_path_from_dentry(direntry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | if (full_path == NULL) { |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 2391 | rc = -ENOMEM; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2392 | free_xid(xid); |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 2393 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | |
Jeff Layton | 0f4d634 | 2009-03-26 13:35:37 -0400 | [diff] [blame] | 2396 | /* |
| 2397 | * Attempt to flush data before changing attributes. We need to do |
| 2398 | * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the |
| 2399 | * ownership or mode then we may also need to do this. Here, we take |
| 2400 | * the safe way out and just do the flush on all setattr requests. If |
| 2401 | * the flush returns error, store it to report later and continue. |
| 2402 | * |
| 2403 | * BB: This should be smarter. Why bother flushing pages that |
| 2404 | * will be truncated anyway? Also, should we error out here if |
| 2405 | * the flush returns error? |
| 2406 | */ |
| 2407 | rc = filemap_write_and_wait(inode->i_mapping); |
Pavel Shilovsky | 9a66396 | 2019-01-08 11:15:28 -0800 | [diff] [blame^] | 2408 | if (is_interrupt_error(rc)) { |
| 2409 | rc = -ERESTARTSYS; |
| 2410 | goto cifs_setattr_exit; |
| 2411 | } |
| 2412 | |
Jeff Layton | eb4b756 | 2010-10-22 14:52:29 -0400 | [diff] [blame] | 2413 | mapping_set_error(inode->i_mapping, rc); |
| 2414 | rc = 0; |
Jeff Layton | cea2180 | 2007-11-20 23:19:03 +0000 | [diff] [blame] | 2415 | |
Steve French | 5053144 | 2008-03-14 19:21:31 +0000 | [diff] [blame] | 2416 | if (attrs->ia_valid & ATTR_SIZE) { |
Jeff Layton | 8efdbde | 2008-07-23 21:28:12 +0000 | [diff] [blame] | 2417 | rc = cifs_set_file_size(inode, attrs, xid, full_path); |
| 2418 | if (rc != 0) |
Steve French | e30dcf3 | 2005-09-20 20:49:16 -0700 | [diff] [blame] | 2419 | goto cifs_setattr_exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | } |
Jeff Layton | 4ca691a | 2008-05-22 09:33:34 -0400 | [diff] [blame] | 2421 | |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 2422 | if (attrs->ia_valid & ATTR_UID) |
| 2423 | uid = attrs->ia_uid; |
| 2424 | |
| 2425 | if (attrs->ia_valid & ATTR_GID) |
| 2426 | gid = attrs->ia_gid; |
| 2427 | |
| 2428 | #ifdef CONFIG_CIFS_ACL |
| 2429 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) { |
Eric W. Biederman | 8abf277 | 2013-02-06 00:33:17 -0800 | [diff] [blame] | 2430 | if (uid_valid(uid) || gid_valid(gid)) { |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 2431 | rc = id_mode_to_cifs_acl(inode, full_path, NO_CHANGE_64, |
| 2432 | uid, gid); |
| 2433 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2434 | cifs_dbg(FYI, "%s: Setting id failed with error: %d\n", |
| 2435 | __func__, rc); |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 2436 | goto cifs_setattr_exit; |
| 2437 | } |
| 2438 | } |
| 2439 | } else |
| 2440 | #endif /* CONFIG_CIFS_ACL */ |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2441 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)) |
Jeff Layton | 4ca691a | 2008-05-22 09:33:34 -0400 | [diff] [blame] | 2442 | attrs->ia_valid &= ~(ATTR_UID | ATTR_GID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | |
Jeff Layton | d32c4f2 | 2007-10-18 03:05:22 -0700 | [diff] [blame] | 2444 | /* skip mode change if it's just for clearing setuid/setgid */ |
| 2445 | if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) |
| 2446 | attrs->ia_valid &= ~ATTR_MODE; |
| 2447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | if (attrs->ia_valid & ATTR_MODE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | mode = attrs->ia_mode; |
Steve French | cdbce9c8 | 2005-11-19 21:04:52 -0800 | [diff] [blame] | 2450 | rc = 0; |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 2451 | #ifdef CONFIG_CIFS_ACL |
Shirish Pargaonkar | 78415d2 | 2010-11-27 11:37:26 -0600 | [diff] [blame] | 2452 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) { |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 2453 | rc = id_mode_to_cifs_acl(inode, full_path, mode, |
Eric W. Biederman | 8abf277 | 2013-02-06 00:33:17 -0800 | [diff] [blame] | 2454 | INVALID_UID, INVALID_GID); |
Shirish Pargaonkar | 78415d2 | 2010-11-27 11:37:26 -0600 | [diff] [blame] | 2455 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2456 | cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n", |
| 2457 | __func__, rc); |
Shirish Pargaonkar | 78415d2 | 2010-11-27 11:37:26 -0600 | [diff] [blame] | 2458 | goto cifs_setattr_exit; |
| 2459 | } |
| 2460 | } else |
Jeff Layton | 79df1ba | 2010-12-06 12:52:08 -0500 | [diff] [blame] | 2461 | #endif /* CONFIG_CIFS_ACL */ |
Jeff Layton | 5132861 | 2008-05-22 09:33:34 -0400 | [diff] [blame] | 2462 | if (((mode & S_IWUGO) == 0) && |
| 2463 | (cifsInode->cifsAttrs & ATTR_READONLY) == 0) { |
Jeff Layton | feb3e20 | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2464 | |
| 2465 | dosattr = cifsInode->cifsAttrs | ATTR_READONLY; |
| 2466 | |
Jeff Layton | 5132861 | 2008-05-22 09:33:34 -0400 | [diff] [blame] | 2467 | /* fix up mode if we're not using dynperm */ |
| 2468 | if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0) |
| 2469 | attrs->ia_mode = inode->i_mode & ~S_IWUGO; |
| 2470 | } else if ((mode & S_IWUGO) && |
| 2471 | (cifsInode->cifsAttrs & ATTR_READONLY)) { |
Jeff Layton | feb3e20 | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2472 | |
| 2473 | dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY; |
| 2474 | /* Attributes of 0 are ignored */ |
| 2475 | if (dosattr == 0) |
| 2476 | dosattr |= ATTR_NORMAL; |
Jeff Layton | 5132861 | 2008-05-22 09:33:34 -0400 | [diff] [blame] | 2477 | |
| 2478 | /* reset local inode permissions to normal */ |
| 2479 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) { |
| 2480 | attrs->ia_mode &= ~(S_IALLUGO); |
| 2481 | if (S_ISDIR(inode->i_mode)) |
| 2482 | attrs->ia_mode |= |
| 2483 | cifs_sb->mnt_dir_mode; |
| 2484 | else |
| 2485 | attrs->ia_mode |= |
| 2486 | cifs_sb->mnt_file_mode; |
| 2487 | } |
| 2488 | } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) { |
| 2489 | /* ignore mode change - ATTR_READONLY hasn't changed */ |
| 2490 | attrs->ia_valid &= ~ATTR_MODE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | } |
| 2493 | |
Jeff Layton | feb3e20 | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2494 | if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) || |
| 2495 | ((attrs->ia_valid & ATTR_MODE) && dosattr)) { |
| 2496 | rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr); |
| 2497 | /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | |
Steve French | e30dcf3 | 2005-09-20 20:49:16 -0700 | [diff] [blame] | 2499 | /* Even if error on time set, no sense failing the call if |
| 2500 | the server would set the time to a reasonable value anyway, |
| 2501 | and this check ensures that we are not being called from |
| 2502 | sys_utimes in which case we ought to fail the call back to |
| 2503 | the user when the server rejects the call */ |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 2504 | if ((rc) && (attrs->ia_valid & |
Jeff Layton | feb3e20 | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2505 | (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE))) |
Steve French | e30dcf3 | 2005-09-20 20:49:16 -0700 | [diff] [blame] | 2506 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | } |
| 2508 | |
| 2509 | /* do not need local check to inode_check_ok since the server does |
| 2510 | that */ |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 2511 | if (rc) |
| 2512 | goto cifs_setattr_exit; |
| 2513 | |
| 2514 | if ((attrs->ia_valid & ATTR_SIZE) && |
Christoph Hellwig | 1b94746 | 2010-07-18 17:51:21 -0400 | [diff] [blame] | 2515 | attrs->ia_size != i_size_read(inode)) |
| 2516 | truncate_setsize(inode, attrs->ia_size); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 2517 | |
| 2518 | setattr_copy(inode, attrs); |
| 2519 | mark_inode_dirty(inode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 2520 | |
Steve French | e30dcf3 | 2005-09-20 20:49:16 -0700 | [diff] [blame] | 2521 | cifs_setattr_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2522 | kfree(full_path); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2523 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2524 | return rc; |
| 2525 | } |
| 2526 | |
Jeff Layton | 0510eeb | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2527 | int |
| 2528 | cifs_setattr(struct dentry *direntry, struct iattr *attrs) |
| 2529 | { |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 2530 | struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb); |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2531 | struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb); |
Jeff Layton | 0510eeb | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2532 | |
| 2533 | if (pTcon->unix_ext) |
| 2534 | return cifs_setattr_unix(direntry, attrs); |
| 2535 | |
| 2536 | return cifs_setattr_nounix(direntry, attrs); |
| 2537 | |
| 2538 | /* BB: add cifs_setattr_legacy for really old servers */ |
| 2539 | } |
| 2540 | |
Steve French | 99ee4db | 2007-02-27 05:35:17 +0000 | [diff] [blame] | 2541 | #if 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | void cifs_delete_inode(struct inode *inode) |
| 2543 | { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2544 | cifs_dbg(FYI, "In cifs_delete_inode, inode = 0x%p\n", inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | /* may have to add back in if and when safe distributed caching of |
| 2546 | directories added e.g. via FindNotify */ |
| 2547 | } |
Steve French | 99ee4db | 2007-02-27 05:35:17 +0000 | [diff] [blame] | 2548 | #endif |