at commit c03c1a14
| 1 | // SPDX-License-Identifier: LGPL-2.1 |
| 2 | /* |
| 3 | * |
| 4 | * Copyright (C) International Business Machines Corp., 2002,2010 |
| 5 | * Author(s): Steve French (sfrench@us.ibm.com) |
| 6 | * |
| 7 | */ |
| 8 | #include <linux/fs.h> |
| 9 | #include <linux/fs_struct.h> |
| 10 | #include <linux/stat.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/pagemap.h> |
| 13 | #include <linux/freezer.h> |
| 14 | #include <linux/sched/signal.h> |
| 15 | #include <linux/wait_bit.h> |
| 16 | #include <linux/fiemap.h> |
| 17 | #include <asm/div64.h> |
| 18 | #include "cifsfs.h" |
| 19 | #include "cifsglob.h" |
| 20 | #include "cifsproto.h" |
| 21 | #include "smb2proto.h" |
| 22 | #include "cifs_debug.h" |
| 23 | #include "cifs_fs_sb.h" |
| 24 | #include "cifs_unicode.h" |
| 25 | #include "fscache.h" |
| 26 | #include "fs_context.h" |
| 27 | #include "cifs_ioctl.h" |
| 28 | #include "cached_dir.h" |
| 29 | #include "reparse.h" |
| 30 | |
| 31 | static void cifs_invalidate_cached_dir(struct cifs_tcon *tcon, |
| 32 | struct dentry *parent) |
| 33 | { |
| 34 | struct cached_fid *parent_cfid = NULL; |
| 35 | |
| 36 | if (!tcon || !parent) |
| 37 | return; |
| 38 | |
| 39 | if (!open_cached_dir_by_dentry(tcon, parent, &parent_cfid)) { |
| 40 | mutex_lock(&parent_cfid->dirents.de_mutex); |
| 41 | parent_cfid->dirents.is_valid = false; |
| 42 | parent_cfid->dirents.is_failed = true; |
| 43 | mutex_unlock(&parent_cfid->dirents.de_mutex); |
| 44 | close_cached_dir(parent_cfid); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /* |
| 49 | * Set parameters for the netfs library |
| 50 | */ |
| 51 | static void cifs_set_netfs_context(struct inode *inode) |
| 52 | { |
| 53 | struct cifsInodeInfo *cifs_i = CIFS_I(inode); |
| 54 | |
| 55 | netfs_inode_init(&cifs_i->netfs, &cifs_req_ops, true); |
| 56 | } |
| 57 | |
| 58 | static void cifs_set_ops(struct inode *inode) |
| 59 | { |
| 60 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode); |
| 61 | struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); |
| 62 | struct netfs_inode *ictx = netfs_inode(inode); |
| 63 | unsigned int sbflags = cifs_sb_flags(cifs_sb); |
| 64 | |
| 65 | switch (inode->i_mode & S_IFMT) { |
| 66 | case S_IFREG: |
| 67 | inode->i_op = &cifs_file_inode_ops; |
| 68 | if (sbflags & CIFS_MOUNT_DIRECT_IO) { |
| 69 | set_bit(NETFS_ICTX_UNBUFFERED, &ictx->flags); |
| 70 | if (sbflags & CIFS_MOUNT_NO_BRL) |
| 71 | inode->i_fop = &cifs_file_direct_nobrl_ops; |
| 72 | else |
| 73 | inode->i_fop = &cifs_file_direct_ops; |
| 74 | } else if (sbflags & CIFS_MOUNT_STRICT_IO) { |
| 75 | if (sbflags & CIFS_MOUNT_NO_BRL) |
| 76 | inode->i_fop = &cifs_file_strict_nobrl_ops; |
| 77 | else |
| 78 | inode->i_fop = &cifs_file_strict_ops; |
| 79 | } else if (sbflags & CIFS_MOUNT_NO_BRL) |
| 80 | inode->i_fop = &cifs_file_nobrl_ops; |
| 81 | else { /* not direct, send byte range locks */ |
| 82 | inode->i_fop = &cifs_file_ops; |
| 83 | } |
| 84 | |
| 85 | /* check if server can support readahead */ |
| 86 | if (tcon->ses->server->max_read < PAGE_SIZE + MAX_CIFS_HDR_SIZE) |
| 87 | inode->i_data.a_ops = &cifs_addr_ops_smallbuf; |
| 88 | else |
| 89 | inode->i_data.a_ops = &cifs_addr_ops; |
| 90 | mapping_set_large_folios(inode->i_mapping); |
| 91 | if (tcon->ses->server->sign) |
| 92 | mapping_set_stable_writes(inode->i_mapping); |
| 93 | break; |
| 94 | case S_IFDIR: |
| 95 | if (IS_AUTOMOUNT(inode)) { |
| 96 | inode->i_op = &cifs_namespace_inode_operations; |
| 97 | } else { |
| 98 | inode->i_op = &cifs_dir_inode_ops; |
| 99 | inode->i_fop = &cifs_dir_ops; |
| 100 | } |
| 101 | break; |
| 102 | case S_IFLNK: |
| 103 | inode->i_op = &cifs_symlink_inode_ops; |
| 104 | break; |
| 105 | default: |
| 106 | init_special_inode(inode, inode->i_mode, inode->i_rdev); |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /* check inode attributes against fattr. If they don't match, tag the |
| 112 | * inode for cache invalidation |
| 113 | */ |
| 114 | static void |
| 115 | cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr) |
| 116 | { |
| 117 | struct cifs_fscache_inode_coherency_data cd; |
| 118 | struct cifsInodeInfo *cifs_i = CIFS_I(inode); |
| 119 | struct timespec64 mtime; |
| 120 | |
| 121 | cifs_dbg(FYI, "%s: revalidating inode %llu\n", |
| 122 | __func__, cifs_i->uniqueid); |
| 123 | |
| 124 | if (inode_state_read_once(inode) & I_NEW) { |
| 125 | cifs_dbg(FYI, "%s: inode %llu is new\n", |
| 126 | __func__, cifs_i->uniqueid); |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | /* don't bother with revalidation if we have an oplock */ |
| 131 | if (CIFS_CACHE_READ(cifs_i)) { |
| 132 | cifs_dbg(FYI, "%s: inode %llu is oplocked\n", |
| 133 | __func__, cifs_i->uniqueid); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | /* revalidate if mtime or size have changed */ |
| 138 | fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode); |
| 139 | mtime = inode_get_mtime(inode); |
| 140 | if (timespec64_equal(&mtime, &fattr->cf_mtime) && |
| 141 | netfs_read_remote_i_size(inode) == fattr->cf_eof) { |
| 142 | cifs_dbg(FYI, "%s: inode %llu is unchanged\n", |
| 143 | __func__, cifs_i->uniqueid); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n", |
| 148 | __func__, cifs_i->uniqueid); |
| 149 | set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags); |
| 150 | /* Invalidate fscache cookie */ |
| 151 | cifs_fscache_fill_coherency(&cifs_i->netfs.inode, &cd); |
| 152 | fscache_invalidate(cifs_inode_cookie(inode), &cd, i_size_read(inode), 0); |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * copy nlink to the inode, unless it wasn't provided. Provide |
| 157 | * sane values if we don't have an existing one and none was provided |
| 158 | */ |
| 159 | static void |
| 160 | cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr) |
| 161 | { |
| 162 | /* |
| 163 | * if we're in a situation where we can't trust what we |
| 164 | * got from the server (readdir, some non-unix cases) |
| 165 | * fake reasonable values |
| 166 | */ |
| 167 | if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) { |
| 168 | /* only provide fake values on a new inode */ |
| 169 | if (inode_state_read_once(inode) & I_NEW) { |
| 170 | if (fattr->cf_cifsattrs & ATTR_DIRECTORY) |
| 171 | set_nlink(inode, 2); |
| 172 | else |
| 173 | set_nlink(inode, 1); |
| 174 | } |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | /* we trust the server, so update it */ |
| 179 | set_nlink(inode, fattr->cf_nlink); |
| 180 | } |
| 181 | |
| 182 | /* populate an inode with info from a cifs_fattr struct */ |
| 183 | int |
| 184 | cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr, |
| 185 | bool from_readdir) |
| 186 | { |
| 187 | struct cifsInodeInfo *cifs_i = CIFS_I(inode); |
| 188 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 189 | |
| 190 | if (!(inode_state_read_once(inode) & I_NEW) && |
| 191 | unlikely(inode_wrong_type(inode, fattr->cf_mode))) { |
| 192 | CIFS_I(inode)->time = 0; /* force reval */ |
| 193 | return -ESTALE; |
| 194 | } |
| 195 | cifs_revalidate_cache(inode, fattr); |
| 196 | |
| 197 | spin_lock(&inode->i_lock); |
| 198 | if (inode_state_read_once(inode) & I_NEW) |
| 199 | netfs_write_zero_point(inode, fattr->cf_eof); |
| 200 | |
| 201 | fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode); |
| 202 | fattr->cf_atime = timestamp_truncate(fattr->cf_atime, inode); |
| 203 | fattr->cf_ctime = timestamp_truncate(fattr->cf_ctime, inode); |
| 204 | /* we do not want atime to be less than mtime, it broke some apps */ |
| 205 | if (timespec64_compare(&fattr->cf_atime, &fattr->cf_mtime) < 0) |
| 206 | inode_set_atime_to_ts(inode, fattr->cf_mtime); |
| 207 | else |
| 208 | inode_set_atime_to_ts(inode, fattr->cf_atime); |
| 209 | inode_set_mtime_to_ts(inode, fattr->cf_mtime); |
| 210 | inode_set_ctime_to_ts(inode, fattr->cf_ctime); |
| 211 | inode->i_rdev = fattr->cf_rdev; |
| 212 | cifs_nlink_fattr_to_inode(inode, fattr); |
| 213 | inode->i_uid = fattr->cf_uid; |
| 214 | inode->i_gid = fattr->cf_gid; |
| 215 | |
| 216 | /* if dynperm is set, don't clobber existing mode */ |
| 217 | if ((inode_state_read(inode) & I_NEW) || |
| 218 | !(cifs_sb_flags(cifs_sb) & CIFS_MOUNT_DYNPERM)) |
| 219 | inode->i_mode = fattr->cf_mode; |
| 220 | |
| 221 | cifs_i->cifsAttrs = fattr->cf_cifsattrs; |
| 222 | cifs_i->reparse_tag = fattr->cf_cifstag; |
| 223 | |
| 224 | if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL) |
| 225 | cifs_i->time = 0; |
| 226 | else |
| 227 | cifs_i->time = jiffies; |
| 228 | |
| 229 | if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING) |
| 230 | set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags); |
| 231 | else |
| 232 | clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags); |
| 233 | |
| 234 | netfs_write_remote_i_size(inode, fattr->cf_eof); |
| 235 | /* |
| 236 | * Can't safely change the file size here if the client is writing to |
| 237 | * it due to potential races. |
| 238 | */ |
| 239 | if (is_size_safe_to_change(cifs_i, fattr->cf_eof, from_readdir)) { |
| 240 | i_size_write(inode, fattr->cf_eof); |
| 241 | inode->i_blocks = CIFS_INO_BLOCKS(fattr->cf_bytes); |
| 242 | } else if (from_readdir && i_size_read(inode) != fattr->cf_eof) { |
| 243 | cifs_i->time = 0; |
| 244 | } |
| 245 | |
| 246 | if (S_ISLNK(fattr->cf_mode) && fattr->cf_symlink_target) { |
| 247 | kfree(cifs_i->symlink_target); |
| 248 | cifs_i->symlink_target = fattr->cf_symlink_target; |
| 249 | fattr->cf_symlink_target = NULL; |
| 250 | } |
| 251 | spin_unlock(&inode->i_lock); |
| 252 | |
| 253 | if (fattr->cf_flags & CIFS_FATTR_JUNCTION) |
| 254 | inode->i_flags |= S_AUTOMOUNT; |
| 255 | if (inode_state_read_once(inode) & I_NEW) { |
| 256 | cifs_set_netfs_context(inode); |
| 257 | cifs_set_ops(inode); |
| 258 | } |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | void |
| 263 | cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr) |
| 264 | { |
| 265 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
| 266 | |
| 267 | if (!(cifs_sb_flags(cifs_sb) & CIFS_MOUNT_SERVER_INUM)) |
| 268 | fattr->cf_uniqueid = iunique(sb, ROOT_I); |
| 269 | } |
| 270 | |
| 271 | /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */ |
| 272 | void |
| 273 | cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info, |
| 274 | struct cifs_sb_info *cifs_sb) |
| 275 | { |
| 276 | unsigned int sbflags; |
| 277 | |
| 278 | memset(fattr, 0, sizeof(*fattr)); |
| 279 | fattr->cf_uniqueid = le64_to_cpu(info->UniqueId); |
| 280 | fattr->cf_bytes = le64_to_cpu(info->NumOfBytes); |
| 281 | fattr->cf_eof = le64_to_cpu(info->EndOfFile); |
| 282 | |
| 283 | fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime); |
| 284 | fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime); |
| 285 | fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange); |
| 286 | /* old POSIX extensions don't get create time */ |
| 287 | |
| 288 | fattr->cf_mode = le64_to_cpu(info->Permissions); |
| 289 | |
| 290 | /* |
| 291 | * Since we set the inode type below we need to mask off |
| 292 | * to avoid strange results if bits set above. |
| 293 | */ |
| 294 | fattr->cf_mode &= ~S_IFMT; |
| 295 | switch (le32_to_cpu(info->Type)) { |
| 296 | case UNIX_FILE: |
| 297 | fattr->cf_mode |= S_IFREG; |
| 298 | fattr->cf_dtype = DT_REG; |
| 299 | break; |
| 300 | case UNIX_SYMLINK: |
| 301 | fattr->cf_mode |= S_IFLNK; |
| 302 | fattr->cf_dtype = DT_LNK; |
| 303 | break; |
| 304 | case UNIX_DIR: |
| 305 | fattr->cf_mode |= S_IFDIR; |
| 306 | fattr->cf_dtype = DT_DIR; |
| 307 | break; |
| 308 | case UNIX_CHARDEV: |
| 309 | fattr->cf_mode |= S_IFCHR; |
| 310 | fattr->cf_dtype = DT_CHR; |
| 311 | fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor), |
| 312 | le64_to_cpu(info->DevMinor) & MINORMASK); |
| 313 | break; |
| 314 | case UNIX_BLOCKDEV: |
| 315 | fattr->cf_mode |= S_IFBLK; |
| 316 | fattr->cf_dtype = DT_BLK; |
| 317 | fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor), |
| 318 | le64_to_cpu(info->DevMinor) & MINORMASK); |
| 319 | break; |
| 320 | case UNIX_FIFO: |
| 321 | fattr->cf_mode |= S_IFIFO; |
| 322 | fattr->cf_dtype = DT_FIFO; |
| 323 | break; |
| 324 | case UNIX_SOCKET: |
| 325 | fattr->cf_mode |= S_IFSOCK; |
| 326 | fattr->cf_dtype = DT_SOCK; |
| 327 | break; |
| 328 | default: |
| 329 | /* safest to call it a file if we do not know */ |
| 330 | fattr->cf_mode |= S_IFREG; |
| 331 | fattr->cf_dtype = DT_REG; |
| 332 | cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type)); |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | sbflags = cifs_sb_flags(cifs_sb); |
| 337 | fattr->cf_uid = cifs_sb->ctx->linux_uid; |
| 338 | if (!(sbflags & CIFS_MOUNT_OVERR_UID)) { |
| 339 | u64 id = le64_to_cpu(info->Uid); |
| 340 | if (id < ((uid_t)-1)) { |
| 341 | kuid_t uid = make_kuid(&init_user_ns, id); |
| 342 | if (uid_valid(uid)) |
| 343 | fattr->cf_uid = uid; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | fattr->cf_gid = cifs_sb->ctx->linux_gid; |
| 348 | if (!(sbflags & CIFS_MOUNT_OVERR_GID)) { |
| 349 | u64 id = le64_to_cpu(info->Gid); |
| 350 | if (id < ((gid_t)-1)) { |
| 351 | kgid_t gid = make_kgid(&init_user_ns, id); |
| 352 | if (gid_valid(gid)) |
| 353 | fattr->cf_gid = gid; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | fattr->cf_nlink = le64_to_cpu(info->Nlinks); |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * Fill a cifs_fattr struct with fake inode info. |
| 362 | * |
| 363 | * Needed to setup cifs_fattr data for the directory which is the |
| 364 | * junction to the new submount (ie to setup the fake directory |
| 365 | * which represents a DFS referral or reparse mount point). |
| 366 | */ |
| 367 | static void cifs_create_junction_fattr(struct cifs_fattr *fattr, |
| 368 | struct super_block *sb) |
| 369 | { |
| 370 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
| 371 | |
| 372 | cifs_dbg(FYI, "%s: creating fake fattr\n", __func__); |
| 373 | |
| 374 | memset(fattr, 0, sizeof(*fattr)); |
| 375 | fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU; |
| 376 | fattr->cf_uid = cifs_sb->ctx->linux_uid; |
| 377 | fattr->cf_gid = cifs_sb->ctx->linux_gid; |
| 378 | ktime_get_coarse_real_ts64(&fattr->cf_mtime); |
| 379 | fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime; |
| 380 | fattr->cf_nlink = 2; |
| 381 | fattr->cf_flags = CIFS_FATTR_JUNCTION; |
| 382 | } |
| 383 | |
| 384 | /* Update inode with final fattr data */ |
| 385 | static int update_inode_info(struct super_block *sb, |
| 386 | struct cifs_fattr *fattr, |
| 387 | struct inode **inode) |
| 388 | { |
| 389 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
| 390 | int rc = 0; |
| 391 | |
| 392 | if (!*inode) { |
| 393 | *inode = cifs_iget(sb, fattr); |
| 394 | if (!*inode) |
| 395 | rc = -ENOMEM; |
| 396 | return rc; |
| 397 | } |
| 398 | /* We already have inode, update it. |
| 399 | * |
| 400 | * If file type or uniqueid is different, return error. |
| 401 | */ |
| 402 | if (unlikely((cifs_sb_flags(cifs_sb) & CIFS_MOUNT_SERVER_INUM) && |
| 403 | CIFS_I(*inode)->uniqueid != fattr->cf_uniqueid)) { |
| 404 | CIFS_I(*inode)->time = 0; /* force reval */ |
| 405 | return -ESTALE; |
| 406 | } |
| 407 | return cifs_fattr_to_inode(*inode, fattr, false); |
| 408 | } |
| 409 | |
| 410 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 411 | static int |
| 412 | cifs_get_file_info_unix(struct file *filp) |
| 413 | { |
| 414 | int rc; |
| 415 | unsigned int xid; |
| 416 | FILE_UNIX_BASIC_INFO find_data; |
| 417 | struct cifs_fattr fattr = {}; |
| 418 | struct inode *inode = file_inode(filp); |
| 419 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 420 | struct cifsFileInfo *cfile = filp->private_data; |
| 421 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
| 422 | |
| 423 | xid = get_xid(); |
| 424 | |
| 425 | if (cfile->symlink_target) { |
| 426 | fattr.cf_symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL); |
| 427 | if (!fattr.cf_symlink_target) { |
| 428 | rc = -ENOMEM; |
| 429 | goto cifs_gfiunix_out; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data); |
| 434 | if (!rc) { |
| 435 | cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb); |
| 436 | } else if (rc == -EREMOTE) { |
| 437 | cifs_create_junction_fattr(&fattr, inode->i_sb); |
| 438 | } else |
| 439 | goto cifs_gfiunix_out; |
| 440 | |
| 441 | rc = cifs_fattr_to_inode(inode, &fattr, false); |
| 442 | |
| 443 | cifs_gfiunix_out: |
| 444 | free_xid(xid); |
| 445 | return rc; |
| 446 | } |
| 447 | |
| 448 | static int cifs_get_unix_fattr(const unsigned char *full_path, |
| 449 | struct super_block *sb, |
| 450 | struct cifs_fattr *fattr, |
| 451 | struct inode **pinode, |
| 452 | const unsigned int xid) |
| 453 | { |
| 454 | struct TCP_Server_Info *server; |
| 455 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
| 456 | FILE_UNIX_BASIC_INFO find_data; |
| 457 | struct cifs_tcon *tcon; |
| 458 | struct tcon_link *tlink; |
| 459 | int rc, tmprc; |
| 460 | |
| 461 | cifs_dbg(FYI, "Getting info on %s\n", full_path); |
| 462 | |
| 463 | tlink = cifs_sb_tlink(cifs_sb); |
| 464 | if (IS_ERR(tlink)) |
| 465 | return PTR_ERR(tlink); |
| 466 | tcon = tlink_tcon(tlink); |
| 467 | server = tcon->ses->server; |
| 468 | |
| 469 | /* could have done a find first instead but this returns more info */ |
| 470 | rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data, |
| 471 | cifs_sb->local_nls, cifs_remap(cifs_sb)); |
| 472 | cifs_dbg(FYI, "%s: query path info: rc = %d\n", __func__, rc); |
| 473 | cifs_put_tlink(tlink); |
| 474 | |
| 475 | if (!rc) { |
| 476 | cifs_unix_basic_to_fattr(fattr, &find_data, cifs_sb); |
| 477 | } else if (rc == -EREMOTE) { |
| 478 | cifs_create_junction_fattr(fattr, sb); |
| 479 | rc = 0; |
| 480 | } else { |
| 481 | return rc; |
| 482 | } |
| 483 | |
| 484 | if (!*pinode) |
| 485 | cifs_fill_uniqueid(sb, fattr); |
| 486 | |
| 487 | /* check for Minshall+French symlinks */ |
| 488 | if (cifs_sb_flags(cifs_sb) & CIFS_MOUNT_MF_SYMLINKS) { |
| 489 | tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path); |
| 490 | cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc); |
| 491 | } |
| 492 | |
| 493 | if (S_ISLNK(fattr->cf_mode) && !fattr->cf_symlink_target) { |
| 494 | if (!server->ops->query_symlink) |
| 495 | return -EOPNOTSUPP; |
| 496 | rc = server->ops->query_symlink(xid, tcon, |
| 497 | cifs_sb, full_path, |
| 498 | &fattr->cf_symlink_target); |
| 499 | cifs_dbg(FYI, "%s: query_symlink: %d\n", __func__, rc); |
| 500 | } |
| 501 | return rc; |
| 502 | } |
| 503 | |
| 504 | int cifs_get_inode_info_unix(struct inode **pinode, |
| 505 | const unsigned char *full_path, |
| 506 | struct super_block *sb, unsigned int xid) |
| 507 | { |
| 508 | struct cifs_fattr fattr = {}; |
| 509 | int rc; |
| 510 | |
| 511 | rc = cifs_get_unix_fattr(full_path, sb, &fattr, pinode, xid); |
| 512 | if (rc) |
| 513 | goto out; |
| 514 | |
| 515 | rc = update_inode_info(sb, &fattr, pinode); |
| 516 | out: |
| 517 | kfree(fattr.cf_symlink_target); |
| 518 | return rc; |
| 519 | } |
| 520 | #else |
| 521 | static inline int cifs_get_unix_fattr(const unsigned char *full_path, |
| 522 | struct super_block *sb, |
| 523 | struct cifs_fattr *fattr, |
| 524 | struct inode **pinode, |
| 525 | const unsigned int xid) |
| 526 | { |
| 527 | return -EOPNOTSUPP; |
| 528 | } |
| 529 | |
| 530 | int cifs_get_inode_info_unix(struct inode **pinode, |
| 531 | const unsigned char *full_path, |
| 532 | struct super_block *sb, unsigned int xid) |
| 533 | { |
| 534 | return -EOPNOTSUPP; |
| 535 | } |
| 536 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 537 | |
| 538 | static int |
| 539 | cifs_sfu_type(struct cifs_fattr *fattr, const char *path, |
| 540 | struct cifs_sb_info *cifs_sb, unsigned int xid) |
| 541 | { |
| 542 | int rc; |
| 543 | __u32 oplock; |
| 544 | struct tcon_link *tlink; |
| 545 | struct cifs_tcon *tcon; |
| 546 | struct cifs_fid fid; |
| 547 | struct cifs_open_parms oparms; |
| 548 | struct cifs_io_parms io_parms = {0}; |
| 549 | char *symlink_buf_utf16; |
| 550 | unsigned int symlink_len_utf16; |
| 551 | char buf[24]; |
| 552 | unsigned int bytes_read; |
| 553 | char *pbuf; |
| 554 | int buf_type = CIFS_NO_BUFFER; |
| 555 | |
| 556 | pbuf = buf; |
| 557 | |
| 558 | fattr->cf_mode &= ~S_IFMT; |
| 559 | |
| 560 | if (fattr->cf_eof == 0) { |
| 561 | cifs_dbg(FYI, "Fifo\n"); |
| 562 | fattr->cf_mode |= S_IFIFO; |
| 563 | fattr->cf_dtype = DT_FIFO; |
| 564 | return 0; |
| 565 | } else if (fattr->cf_eof > 1 && fattr->cf_eof < 8) { |
| 566 | fattr->cf_mode |= S_IFREG; |
| 567 | fattr->cf_dtype = DT_REG; |
| 568 | return -EINVAL; /* EOPNOTSUPP? */ |
| 569 | } |
| 570 | |
| 571 | tlink = cifs_sb_tlink(cifs_sb); |
| 572 | if (IS_ERR(tlink)) |
| 573 | return PTR_ERR(tlink); |
| 574 | tcon = tlink_tcon(tlink); |
| 575 | |
| 576 | oparms = (struct cifs_open_parms) { |
| 577 | .tcon = tcon, |
| 578 | .cifs_sb = cifs_sb, |
| 579 | .desired_access = GENERIC_READ, |
| 580 | .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR), |
| 581 | .disposition = FILE_OPEN, |
| 582 | .path = path, |
| 583 | .fid = &fid, |
| 584 | }; |
| 585 | |
| 586 | if (tcon->ses->server->oplocks) |
| 587 | oplock = REQ_OPLOCK; |
| 588 | else |
| 589 | oplock = 0; |
| 590 | rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL); |
| 591 | if (rc) { |
| 592 | cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc); |
| 593 | cifs_put_tlink(tlink); |
| 594 | return rc; |
| 595 | } |
| 596 | |
| 597 | /* Read header */ |
| 598 | io_parms.netfid = fid.netfid; |
| 599 | io_parms.pid = current->tgid; |
| 600 | io_parms.tcon = tcon; |
| 601 | io_parms.offset = 0; |
| 602 | io_parms.length = 24; |
| 603 | |
| 604 | rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms, |
| 605 | &bytes_read, &pbuf, &buf_type); |
| 606 | if ((rc == 0) && (bytes_read >= 8)) { |
| 607 | if (memcmp("IntxBLK\0", pbuf, 8) == 0) { |
| 608 | cifs_dbg(FYI, "Block device\n"); |
| 609 | fattr->cf_mode |= S_IFBLK; |
| 610 | fattr->cf_dtype = DT_BLK; |
| 611 | if (bytes_read == 24) { |
| 612 | /* we have enough to decode dev num */ |
| 613 | __u64 mjr; /* major */ |
| 614 | __u64 mnr; /* minor */ |
| 615 | mjr = le64_to_cpu(*(__le64 *)(pbuf+8)); |
| 616 | mnr = le64_to_cpu(*(__le64 *)(pbuf+16)); |
| 617 | fattr->cf_rdev = MKDEV(mjr, mnr); |
| 618 | } else if (bytes_read == 16) { |
| 619 | /* |
| 620 | * Windows NFS server before Windows Server 2012 |
| 621 | * stores major and minor number in SFU-modified |
| 622 | * style, just as 32-bit numbers. Recognize it. |
| 623 | */ |
| 624 | __u32 mjr; /* major */ |
| 625 | __u32 mnr; /* minor */ |
| 626 | mjr = le32_to_cpu(*(__le32 *)(pbuf+8)); |
| 627 | mnr = le32_to_cpu(*(__le32 *)(pbuf+12)); |
| 628 | fattr->cf_rdev = MKDEV(mjr, mnr); |
| 629 | } |
| 630 | } else if (memcmp("IntxCHR\0", pbuf, 8) == 0) { |
| 631 | cifs_dbg(FYI, "Char device\n"); |
| 632 | fattr->cf_mode |= S_IFCHR; |
| 633 | fattr->cf_dtype = DT_CHR; |
| 634 | if (bytes_read == 24) { |
| 635 | /* we have enough to decode dev num */ |
| 636 | __u64 mjr; /* major */ |
| 637 | __u64 mnr; /* minor */ |
| 638 | mjr = le64_to_cpu(*(__le64 *)(pbuf+8)); |
| 639 | mnr = le64_to_cpu(*(__le64 *)(pbuf+16)); |
| 640 | fattr->cf_rdev = MKDEV(mjr, mnr); |
| 641 | } else if (bytes_read == 16) { |
| 642 | /* |
| 643 | * Windows NFS server before Windows Server 2012 |
| 644 | * stores major and minor number in SFU-modified |
| 645 | * style, just as 32-bit numbers. Recognize it. |
| 646 | */ |
| 647 | __u32 mjr; /* major */ |
| 648 | __u32 mnr; /* minor */ |
| 649 | mjr = le32_to_cpu(*(__le32 *)(pbuf+8)); |
| 650 | mnr = le32_to_cpu(*(__le32 *)(pbuf+12)); |
| 651 | fattr->cf_rdev = MKDEV(mjr, mnr); |
| 652 | } |
| 653 | } else if (memcmp("LnxSOCK", pbuf, 8) == 0) { |
| 654 | cifs_dbg(FYI, "Socket\n"); |
| 655 | fattr->cf_mode |= S_IFSOCK; |
| 656 | fattr->cf_dtype = DT_SOCK; |
| 657 | } else if (memcmp("IntxLNK\1", pbuf, 8) == 0) { |
| 658 | cifs_dbg(FYI, "Symlink\n"); |
| 659 | fattr->cf_mode |= S_IFLNK; |
| 660 | fattr->cf_dtype = DT_LNK; |
| 661 | if ((fattr->cf_eof > 8) && (fattr->cf_eof % 2 == 0)) { |
| 662 | symlink_buf_utf16 = kmalloc(fattr->cf_eof-8 + 1, GFP_KERNEL); |
| 663 | if (symlink_buf_utf16) { |
| 664 | io_parms.offset = 8; |
| 665 | io_parms.length = fattr->cf_eof-8 + 1; |
| 666 | buf_type = CIFS_NO_BUFFER; |
| 667 | rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms, |
| 668 | &symlink_len_utf16, |
| 669 | &symlink_buf_utf16, |
| 670 | &buf_type); |
| 671 | /* |
| 672 | * Check that read buffer has valid length and does not |
| 673 | * contain UTF-16 null codepoint (via UniStrnlen() call) |
| 674 | * because Linux cannot process symlink with null byte. |
| 675 | */ |
| 676 | if ((rc == 0) && |
| 677 | (symlink_len_utf16 > 0) && |
| 678 | (symlink_len_utf16 < fattr->cf_eof-8 + 1) && |
| 679 | (symlink_len_utf16 % 2 == 0) && |
| 680 | (UniStrnlen((wchar_t *)symlink_buf_utf16, symlink_len_utf16/2) == symlink_len_utf16/2)) { |
| 681 | fattr->cf_symlink_target = |
| 682 | cifs_strndup_from_utf16(symlink_buf_utf16, |
| 683 | symlink_len_utf16, |
| 684 | true, |
| 685 | cifs_sb->local_nls); |
| 686 | if (!fattr->cf_symlink_target) |
| 687 | rc = -ENOMEM; |
| 688 | } |
| 689 | kfree(symlink_buf_utf16); |
| 690 | } else { |
| 691 | rc = -ENOMEM; |
| 692 | } |
| 693 | } |
| 694 | } else if (memcmp("LnxFIFO", pbuf, 8) == 0) { |
| 695 | cifs_dbg(FYI, "FIFO\n"); |
| 696 | fattr->cf_mode |= S_IFIFO; |
| 697 | fattr->cf_dtype = DT_FIFO; |
| 698 | } else { |
| 699 | fattr->cf_mode |= S_IFREG; /* file? */ |
| 700 | fattr->cf_dtype = DT_REG; |
| 701 | rc = -EOPNOTSUPP; |
| 702 | } |
| 703 | } else if ((rc == 0) && (bytes_read == 1) && (pbuf[0] == '\0')) { |
| 704 | cifs_dbg(FYI, "Socket\n"); |
| 705 | fattr->cf_mode |= S_IFSOCK; |
| 706 | fattr->cf_dtype = DT_SOCK; |
| 707 | } else { |
| 708 | fattr->cf_mode |= S_IFREG; /* then it is a file */ |
| 709 | fattr->cf_dtype = DT_REG; |
| 710 | rc = -EOPNOTSUPP; /* or some unknown SFU type */ |
| 711 | } |
| 712 | |
| 713 | tcon->ses->server->ops->close(xid, tcon, &fid); |
| 714 | cifs_put_tlink(tlink); |
| 715 | return rc; |
| 716 | } |
| 717 | |
| 718 | #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */ |
| 719 | |
| 720 | /* |
| 721 | * Fetch mode bits as provided by SFU. |
| 722 | * |
| 723 | * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ? |
| 724 | */ |
| 725 | static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path, |
| 726 | struct cifs_sb_info *cifs_sb, unsigned int xid) |
| 727 | { |
| 728 | #ifdef CONFIG_CIFS_XATTR |
| 729 | ssize_t rc; |
| 730 | char ea_value[4]; |
| 731 | __u32 mode; |
| 732 | struct tcon_link *tlink; |
| 733 | struct cifs_tcon *tcon; |
| 734 | |
| 735 | tlink = cifs_sb_tlink(cifs_sb); |
| 736 | if (IS_ERR(tlink)) |
| 737 | return PTR_ERR(tlink); |
| 738 | tcon = tlink_tcon(tlink); |
| 739 | |
| 740 | if (tcon->ses->server->ops->query_all_EAs == NULL) { |
| 741 | cifs_put_tlink(tlink); |
| 742 | return -EOPNOTSUPP; |
| 743 | } |
| 744 | |
| 745 | rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path, |
| 746 | "SETFILEBITS", ea_value, 4 /* size of buf */, |
| 747 | cifs_sb); |
| 748 | cifs_put_tlink(tlink); |
| 749 | if (rc < 0) |
| 750 | return (int)rc; |
| 751 | else if (rc > 3) { |
| 752 | mode = le32_to_cpu(*((__le32 *)ea_value)); |
| 753 | fattr->cf_mode &= ~SFBITS_MASK; |
| 754 | cifs_dbg(FYI, "special bits 0%o org mode 0%o\n", |
| 755 | mode, fattr->cf_mode); |
| 756 | fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode; |
| 757 | cifs_dbg(FYI, "special mode bits 0%o\n", mode); |
| 758 | } |
| 759 | |
| 760 | return 0; |
| 761 | #else |
| 762 | return -EOPNOTSUPP; |
| 763 | #endif |
| 764 | } |
| 765 | |
| 766 | #define POSIX_TYPE_FILE 0 |
| 767 | #define POSIX_TYPE_DIR 1 |
| 768 | #define POSIX_TYPE_SYMLINK 2 |
| 769 | #define POSIX_TYPE_CHARDEV 3 |
| 770 | #define POSIX_TYPE_BLKDEV 4 |
| 771 | #define POSIX_TYPE_FIFO 5 |
| 772 | #define POSIX_TYPE_SOCKET 6 |
| 773 | |
| 774 | #define POSIX_X_OTH 0000001 |
| 775 | #define POSIX_W_OTH 0000002 |
| 776 | #define POSIX_R_OTH 0000004 |
| 777 | #define POSIX_X_GRP 0000010 |
| 778 | #define POSIX_W_GRP 0000020 |
| 779 | #define POSIX_R_GRP 0000040 |
| 780 | #define POSIX_X_USR 0000100 |
| 781 | #define POSIX_W_USR 0000200 |
| 782 | #define POSIX_R_USR 0000400 |
| 783 | #define POSIX_STICKY 0001000 |
| 784 | #define POSIX_SET_GID 0002000 |
| 785 | #define POSIX_SET_UID 0004000 |
| 786 | |
| 787 | #define POSIX_OTH_MASK 0000007 |
| 788 | #define POSIX_GRP_MASK 0000070 |
| 789 | #define POSIX_USR_MASK 0000700 |
| 790 | #define POSIX_PERM_MASK 0000777 |
| 791 | #define POSIX_FILETYPE_MASK 0070000 |
| 792 | |
| 793 | #define POSIX_FILETYPE_SHIFT 12 |
| 794 | |
| 795 | static u32 wire_perms_to_posix(u32 wire) |
| 796 | { |
| 797 | u32 mode = 0; |
| 798 | |
| 799 | mode |= (wire & POSIX_X_OTH) ? S_IXOTH : 0; |
| 800 | mode |= (wire & POSIX_W_OTH) ? S_IWOTH : 0; |
| 801 | mode |= (wire & POSIX_R_OTH) ? S_IROTH : 0; |
| 802 | mode |= (wire & POSIX_X_GRP) ? S_IXGRP : 0; |
| 803 | mode |= (wire & POSIX_W_GRP) ? S_IWGRP : 0; |
| 804 | mode |= (wire & POSIX_R_GRP) ? S_IRGRP : 0; |
| 805 | mode |= (wire & POSIX_X_USR) ? S_IXUSR : 0; |
| 806 | mode |= (wire & POSIX_W_USR) ? S_IWUSR : 0; |
| 807 | mode |= (wire & POSIX_R_USR) ? S_IRUSR : 0; |
| 808 | mode |= (wire & POSIX_STICKY) ? S_ISVTX : 0; |
| 809 | mode |= (wire & POSIX_SET_GID) ? S_ISGID : 0; |
| 810 | mode |= (wire & POSIX_SET_UID) ? S_ISUID : 0; |
| 811 | |
| 812 | return mode; |
| 813 | } |
| 814 | |
| 815 | static u32 posix_filetypes[] = { |
| 816 | S_IFREG, |
| 817 | S_IFDIR, |
| 818 | S_IFLNK, |
| 819 | S_IFCHR, |
| 820 | S_IFBLK, |
| 821 | S_IFIFO, |
| 822 | S_IFSOCK |
| 823 | }; |
| 824 | |
| 825 | static u32 wire_filetype_to_posix(u32 wire_type) |
| 826 | { |
| 827 | if (wire_type >= ARRAY_SIZE(posix_filetypes)) { |
| 828 | pr_warn("Unexpected type %u", wire_type); |
| 829 | return 0; |
| 830 | } |
| 831 | return posix_filetypes[wire_type]; |
| 832 | } |
| 833 | |
| 834 | umode_t wire_mode_to_posix(u32 wire, bool is_dir) |
| 835 | { |
| 836 | u32 wire_type; |
| 837 | u32 mode; |
| 838 | |
| 839 | wire_type = (wire & POSIX_FILETYPE_MASK) >> POSIX_FILETYPE_SHIFT; |
| 840 | /* older servers do not set POSIX file type in the mode field in the response */ |
| 841 | if ((wire_type == 0) && is_dir) |
| 842 | mode = wire_perms_to_posix(wire) | S_IFDIR; |
| 843 | else |
| 844 | mode = (wire_perms_to_posix(wire) | wire_filetype_to_posix(wire_type)); |
| 845 | return (umode_t)mode; |
| 846 | } |
| 847 | |
| 848 | /* Fill a cifs_fattr struct with info from POSIX info struct */ |
| 849 | static void smb311_posix_info_to_fattr(struct cifs_fattr *fattr, |
| 850 | struct cifs_open_info_data *data, |
| 851 | struct super_block *sb) |
| 852 | { |
| 853 | struct smb311_posix_qinfo *info = &data->posix_fi; |
| 854 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
| 855 | struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); |
| 856 | unsigned int sbflags = cifs_sb_flags(cifs_sb); |
| 857 | |
| 858 | memset(fattr, 0, sizeof(*fattr)); |
| 859 | |
| 860 | /* no fattr->flags to set */ |
| 861 | fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes); |
| 862 | fattr->cf_uniqueid = le64_to_cpu(info->Inode); |
| 863 | |
| 864 | if (info->LastAccessTime) |
| 865 | fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime); |
| 866 | else |
| 867 | ktime_get_coarse_real_ts64(&fattr->cf_atime); |
| 868 | |
| 869 | fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime); |
| 870 | fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime); |
| 871 | |
| 872 | if (data->adjust_tz) { |
| 873 | fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj; |
| 874 | fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj; |
| 875 | } |
| 876 | |
| 877 | /* |
| 878 | * The srv fs device id is overridden on network mount so setting |
| 879 | * @fattr->cf_rdev isn't needed here. |
| 880 | */ |
| 881 | fattr->cf_eof = le64_to_cpu(info->EndOfFile); |
| 882 | fattr->cf_bytes = le64_to_cpu(info->AllocationSize); |
| 883 | fattr->cf_createtime = le64_to_cpu(info->CreationTime); |
| 884 | fattr->cf_nlink = le32_to_cpu(info->HardLinks); |
| 885 | fattr->cf_mode = wire_mode_to_posix(le32_to_cpu(info->Mode), |
| 886 | fattr->cf_cifsattrs & ATTR_DIRECTORY); |
| 887 | |
| 888 | if (cifs_open_data_reparse(data) && |
| 889 | cifs_reparse_point_to_fattr(cifs_sb, fattr, data)) |
| 890 | goto out_reparse; |
| 891 | |
| 892 | fattr->cf_dtype = S_DT(fattr->cf_mode); |
| 893 | |
| 894 | out_reparse: |
| 895 | if (S_ISLNK(fattr->cf_mode)) { |
| 896 | if (likely(data->symlink_target)) |
| 897 | fattr->cf_eof = strnlen(data->symlink_target, PATH_MAX); |
| 898 | fattr->cf_symlink_target = data->symlink_target; |
| 899 | data->symlink_target = NULL; |
| 900 | } |
| 901 | fattr->cf_uid = cifs_sb->ctx->linux_uid; |
| 902 | fattr->cf_gid = cifs_sb->ctx->linux_gid; |
| 903 | if (!(sbflags & CIFS_MOUNT_OVERR_UID)) |
| 904 | sid_to_id(cifs_sb, &data->posix_owner, fattr, SIDOWNER); |
| 905 | if (!(sbflags & CIFS_MOUNT_OVERR_GID)) |
| 906 | sid_to_id(cifs_sb, &data->posix_group, fattr, SIDGROUP); |
| 907 | |
| 908 | cifs_dbg(FYI, "POSIX query info: mode 0x%x uniqueid 0x%llx nlink %d\n", |
| 909 | fattr->cf_mode, fattr->cf_uniqueid, fattr->cf_nlink); |
| 910 | } |
| 911 | |
| 912 | static void cifs_open_info_to_fattr(struct cifs_fattr *fattr, |
| 913 | struct cifs_open_info_data *data, |
| 914 | struct super_block *sb) |
| 915 | { |
| 916 | struct smb2_file_all_info *info = &data->fi; |
| 917 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
| 918 | struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); |
| 919 | |
| 920 | memset(fattr, 0, sizeof(*fattr)); |
| 921 | if (data->unknown_nlink) |
| 922 | fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK; |
| 923 | fattr->cf_cifsattrs = le32_to_cpu(info->Attributes); |
| 924 | if (info->DeletePending) |
| 925 | fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING; |
| 926 | |
| 927 | if (info->LastAccessTime) |
| 928 | fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime); |
| 929 | else |
| 930 | ktime_get_coarse_real_ts64(&fattr->cf_atime); |
| 931 | |
| 932 | fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime); |
| 933 | fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime); |
| 934 | |
| 935 | if (data->adjust_tz) { |
| 936 | fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj; |
| 937 | fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj; |
| 938 | } |
| 939 | |
| 940 | fattr->cf_eof = le64_to_cpu(info->EndOfFile); |
| 941 | fattr->cf_bytes = le64_to_cpu(info->AllocationSize); |
| 942 | fattr->cf_createtime = le64_to_cpu(info->CreationTime); |
| 943 | fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks); |
| 944 | fattr->cf_uid = cifs_sb->ctx->linux_uid; |
| 945 | fattr->cf_gid = cifs_sb->ctx->linux_gid; |
| 946 | |
| 947 | fattr->cf_mode = cifs_sb->ctx->file_mode; |
| 948 | if (cifs_open_data_reparse(data) && |
| 949 | cifs_reparse_point_to_fattr(cifs_sb, fattr, data)) |
| 950 | goto out_reparse; |
| 951 | |
| 952 | if (fattr->cf_cifsattrs & ATTR_DIRECTORY) { |
| 953 | fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode; |
| 954 | fattr->cf_dtype = DT_DIR; |
| 955 | /* |
| 956 | * Server can return wrong NumberOfLinks value for directories |
| 957 | * when Unix extensions are disabled - fake it. |
| 958 | */ |
| 959 | if (!tcon->unix_ext) |
| 960 | fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK; |
| 961 | } else { |
| 962 | fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode; |
| 963 | fattr->cf_dtype = DT_REG; |
| 964 | |
| 965 | /* |
| 966 | * Don't accept zero nlink from non-unix servers unless |
| 967 | * delete is pending. Instead mark it as unknown. |
| 968 | */ |
| 969 | if ((fattr->cf_nlink < 1) && !tcon->unix_ext && |
| 970 | !info->DeletePending) { |
| 971 | cifs_dbg(VFS, "bogus file nlink value %u\n", |
| 972 | fattr->cf_nlink); |
| 973 | fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK; |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | /* clear write bits if ATTR_READONLY is set */ |
| 978 | if (fattr->cf_cifsattrs & ATTR_READONLY) |
| 979 | fattr->cf_mode &= ~(S_IWUGO); |
| 980 | |
| 981 | out_reparse: |
| 982 | if (S_ISLNK(fattr->cf_mode)) { |
| 983 | if (likely(data->symlink_target)) |
| 984 | fattr->cf_eof = strnlen(data->symlink_target, PATH_MAX); |
| 985 | fattr->cf_symlink_target = data->symlink_target; |
| 986 | data->symlink_target = NULL; |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | static int |
| 991 | cifs_get_file_info(struct file *filp) |
| 992 | { |
| 993 | int rc; |
| 994 | unsigned int xid; |
| 995 | struct cifs_open_info_data data = {}; |
| 996 | struct cifs_fattr fattr; |
| 997 | struct inode *inode = file_inode(filp); |
| 998 | struct cifsFileInfo *cfile = filp->private_data; |
| 999 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
| 1000 | struct TCP_Server_Info *server = tcon->ses->server; |
| 1001 | struct dentry *dentry = filp->f_path.dentry; |
| 1002 | void *page = alloc_dentry_path(); |
| 1003 | const unsigned char *path; |
| 1004 | |
| 1005 | if (!server->ops->query_file_info) { |
| 1006 | free_dentry_path(page); |
| 1007 | return -ENOSYS; |
| 1008 | } |
| 1009 | |
| 1010 | xid = get_xid(); |
| 1011 | rc = server->ops->query_file_info(xid, tcon, cfile, &data); |
| 1012 | switch (rc) { |
| 1013 | case 0: |
| 1014 | /* TODO: add support to query reparse tag */ |
| 1015 | data.adjust_tz = false; |
| 1016 | if (data.symlink_target) { |
| 1017 | data.reparse_point = true; |
| 1018 | data.reparse.tag = IO_REPARSE_TAG_SYMLINK; |
| 1019 | } |
| 1020 | path = build_path_from_dentry(dentry, page); |
| 1021 | if (IS_ERR(path)) { |
| 1022 | rc = PTR_ERR(path); |
| 1023 | goto cgfi_exit; |
| 1024 | } |
| 1025 | cifs_open_info_to_fattr(&fattr, &data, inode->i_sb); |
| 1026 | if (fattr.cf_flags & CIFS_FATTR_DELETE_PENDING) |
| 1027 | cifs_mark_open_handles_for_deleted_file(inode, path); |
| 1028 | break; |
| 1029 | case -EREMOTE: |
| 1030 | cifs_create_junction_fattr(&fattr, inode->i_sb); |
| 1031 | break; |
| 1032 | case -EOPNOTSUPP: |
| 1033 | case -EINVAL: |
| 1034 | /* |
| 1035 | * FIXME: legacy server -- fall back to path-based call? |
| 1036 | * for now, just skip revalidating and mark inode for |
| 1037 | * immediate reval. |
| 1038 | */ |
| 1039 | rc = 0; |
| 1040 | CIFS_I(inode)->time = 0; |
| 1041 | goto cgfi_exit; |
| 1042 | default: |
| 1043 | goto cgfi_exit; |
| 1044 | } |
| 1045 | |
| 1046 | /* |
| 1047 | * don't bother with SFU junk here -- just mark inode as needing |
| 1048 | * revalidation. |
| 1049 | */ |
| 1050 | fattr.cf_uniqueid = CIFS_I(inode)->uniqueid; |
| 1051 | fattr.cf_flags |= CIFS_FATTR_NEED_REVAL; |
| 1052 | /* if filetype is different, return error */ |
| 1053 | rc = cifs_fattr_to_inode(inode, &fattr, false); |
| 1054 | cgfi_exit: |
| 1055 | cifs_free_open_info(&data); |
| 1056 | free_dentry_path(page); |
| 1057 | free_xid(xid); |
| 1058 | return rc; |
| 1059 | } |
| 1060 | |
| 1061 | /* Simple function to return a 64 bit hash of string. Rarely called */ |
| 1062 | static __u64 simple_hashstr(const char *str) |
| 1063 | { |
| 1064 | const __u64 hash_mult = 1125899906842597ULL; /* a big enough prime */ |
| 1065 | __u64 hash = 0; |
| 1066 | |
| 1067 | while (*str) |
| 1068 | hash = (hash + (__u64) *str++) * hash_mult; |
| 1069 | |
| 1070 | return hash; |
| 1071 | } |
| 1072 | |
| 1073 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 1074 | /** |
| 1075 | * cifs_backup_query_path_info - SMB1 fallback code to get ino |
| 1076 | * |
| 1077 | * Fallback code to get file metadata when we don't have access to |
| 1078 | * full_path (EACCES) and have backup creds. |
| 1079 | * |
| 1080 | * @xid: transaction id used to identify original request in logs |
| 1081 | * @tcon: information about the server share we have mounted |
| 1082 | * @sb: the superblock stores info such as disk space available |
| 1083 | * @full_path: name of the file we are getting the metadata for |
| 1084 | * @resp_buf: will be set to cifs resp buf and needs to be freed with |
| 1085 | * cifs_buf_release() when done with @data |
| 1086 | * @data: will be set to search info result buffer |
| 1087 | */ |
| 1088 | static int |
| 1089 | cifs_backup_query_path_info(int xid, |
| 1090 | struct cifs_tcon *tcon, |
| 1091 | struct super_block *sb, |
| 1092 | const char *full_path, |
| 1093 | void **resp_buf, |
| 1094 | FILE_ALL_INFO **data) |
| 1095 | { |
| 1096 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
| 1097 | struct cifs_search_info info = {0}; |
| 1098 | u16 flags; |
| 1099 | int rc; |
| 1100 | |
| 1101 | *resp_buf = NULL; |
| 1102 | info.endOfSearch = false; |
| 1103 | if (tcon->unix_ext) |
| 1104 | info.info_level = SMB_FIND_FILE_UNIX; |
| 1105 | else if ((tcon->ses->capabilities & |
| 1106 | tcon->ses->server->vals->cap_nt_find) == 0) |
| 1107 | info.info_level = SMB_FIND_FILE_INFO_STANDARD; |
| 1108 | else if (cifs_sb_flags(cifs_sb) & CIFS_MOUNT_SERVER_INUM) |
| 1109 | info.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO; |
| 1110 | else /* no srvino useful for fallback to some netapp */ |
| 1111 | info.info_level = SMB_FIND_FILE_DIRECTORY_INFO; |
| 1112 | |
| 1113 | flags = CIFS_SEARCH_CLOSE_ALWAYS | |
| 1114 | CIFS_SEARCH_CLOSE_AT_END | |
| 1115 | CIFS_SEARCH_BACKUP_SEARCH; |
| 1116 | |
| 1117 | rc = CIFSFindFirst(xid, tcon, full_path, |
| 1118 | cifs_sb, NULL, flags, &info, false); |
| 1119 | if (rc) |
| 1120 | return rc; |
| 1121 | |
| 1122 | *resp_buf = (void *)info.ntwrk_buf_start; |
| 1123 | *data = (FILE_ALL_INFO *)info.srch_entries_start; |
| 1124 | return 0; |
| 1125 | } |
| 1126 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 1127 | |
| 1128 | static void cifs_set_fattr_ino(int xid, struct cifs_tcon *tcon, struct super_block *sb, |
| 1129 | struct inode **inode, const char *full_path, |
| 1130 | struct cifs_open_info_data *data, struct cifs_fattr *fattr) |
| 1131 | { |
| 1132 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
| 1133 | struct TCP_Server_Info *server = tcon->ses->server; |
| 1134 | int rc; |
| 1135 | |
| 1136 | if (!(cifs_sb_flags(cifs_sb) & CIFS_MOUNT_SERVER_INUM)) { |
| 1137 | if (*inode) |
| 1138 | fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid; |
| 1139 | else |
| 1140 | fattr->cf_uniqueid = iunique(sb, ROOT_I); |
| 1141 | return; |
| 1142 | } |
| 1143 | |
| 1144 | /* |
| 1145 | * If we have an inode pass a NULL tcon to ensure we don't |
| 1146 | * make a round trip to the server. This only works for SMB2+. |
| 1147 | */ |
| 1148 | rc = server->ops->get_srv_inum(xid, *inode ? NULL : tcon, cifs_sb, full_path, |
| 1149 | &fattr->cf_uniqueid, data); |
| 1150 | if (rc) { |
| 1151 | /* |
| 1152 | * If that fails reuse existing ino or generate one |
| 1153 | * and disable server ones |
| 1154 | */ |
| 1155 | if (*inode) |
| 1156 | fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid; |
| 1157 | else { |
| 1158 | fattr->cf_uniqueid = iunique(sb, ROOT_I); |
| 1159 | cifs_autodisable_serverino(cifs_sb, "Cannot retrieve inode number via get_srv_inum", rc); |
| 1160 | } |
| 1161 | return; |
| 1162 | } |
| 1163 | |
| 1164 | /* If no errors, check for zero root inode (invalid) */ |
| 1165 | if (fattr->cf_uniqueid == 0 && strlen(full_path) == 0) { |
| 1166 | cifs_dbg(FYI, "Invalid (0) inodenum\n"); |
| 1167 | if (*inode) { |
| 1168 | /* reuse */ |
| 1169 | fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid; |
| 1170 | } else { |
| 1171 | /* make an ino by hashing the UNC */ |
| 1172 | fattr->cf_flags |= CIFS_FATTR_FAKE_ROOT_INO; |
| 1173 | fattr->cf_uniqueid = simple_hashstr(tcon->tree_name); |
| 1174 | } |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | static inline bool is_inode_cache_good(struct inode *ino) |
| 1179 | { |
| 1180 | return ino && CIFS_CACHE_READ(CIFS_I(ino)) && CIFS_I(ino)->time != 0; |
| 1181 | } |
| 1182 | |
| 1183 | static int reparse_info_to_fattr(struct cifs_open_info_data *data, |
| 1184 | struct super_block *sb, |
| 1185 | const unsigned int xid, |
| 1186 | struct cifs_tcon *tcon, |
| 1187 | const char *full_path, |
| 1188 | struct cifs_fattr *fattr) |
| 1189 | { |
| 1190 | struct TCP_Server_Info *server = tcon->ses->server; |
| 1191 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
| 1192 | struct kvec rsp_iov, *iov = NULL; |
| 1193 | int rsp_buftype = CIFS_NO_BUFFER; |
| 1194 | u32 tag = data->reparse.tag; |
| 1195 | int rc = 0; |
| 1196 | |
| 1197 | if (!tag && server->ops->query_reparse_point) { |
| 1198 | rc = server->ops->query_reparse_point(xid, tcon, cifs_sb, |
| 1199 | full_path, &tag, |
| 1200 | &rsp_iov, &rsp_buftype); |
| 1201 | if (!rc) |
| 1202 | iov = &rsp_iov; |
| 1203 | } else if (data->reparse.io.buftype != CIFS_NO_BUFFER && |
| 1204 | data->reparse.io.iov.iov_base) { |
| 1205 | iov = &data->reparse.io.iov; |
| 1206 | } |
| 1207 | |
| 1208 | rc = -EOPNOTSUPP; |
| 1209 | data->reparse.tag = tag; |
| 1210 | if (!data->reparse.tag) { |
| 1211 | if (server->ops->query_symlink) { |
| 1212 | rc = server->ops->query_symlink(xid, tcon, |
| 1213 | cifs_sb, full_path, |
| 1214 | &data->symlink_target); |
| 1215 | } |
| 1216 | if (rc == -EOPNOTSUPP) |
| 1217 | data->reparse.tag = IO_REPARSE_TAG_INTERNAL; |
| 1218 | } |
| 1219 | |
| 1220 | switch (data->reparse.tag) { |
| 1221 | case 0: /* SMB1 symlink */ |
| 1222 | break; |
| 1223 | case IO_REPARSE_TAG_INTERNAL: |
| 1224 | rc = 0; |
| 1225 | if (cifs_open_data_attrs(data) & ATTR_DIRECTORY) { |
| 1226 | cifs_create_junction_fattr(fattr, sb); |
| 1227 | goto out; |
| 1228 | } |
| 1229 | break; |
| 1230 | default: |
| 1231 | /* Check for cached reparse point data */ |
| 1232 | if (data->symlink_target || data->reparse.buf) { |
| 1233 | rc = 0; |
| 1234 | } else if (iov && server->ops->get_reparse_point_buffer) { |
| 1235 | struct reparse_data_buffer *reparse_buf; |
| 1236 | u32 reparse_len; |
| 1237 | |
| 1238 | reparse_buf = server->ops->get_reparse_point_buffer(iov, &reparse_len); |
| 1239 | rc = parse_reparse_point(reparse_buf, reparse_len, |
| 1240 | cifs_sb, full_path, data); |
| 1241 | /* |
| 1242 | * If the reparse point was not handled but it is the |
| 1243 | * name surrogate which points to directory, then treat |
| 1244 | * is as a new mount point. Name surrogate reparse point |
| 1245 | * represents another named entity in the system. |
| 1246 | */ |
| 1247 | if (rc == -EOPNOTSUPP && |
| 1248 | IS_REPARSE_TAG_NAME_SURROGATE(data->reparse.tag) && |
| 1249 | (cifs_open_data_attrs(data) & ATTR_DIRECTORY)) { |
| 1250 | rc = 0; |
| 1251 | cifs_create_junction_fattr(fattr, sb); |
| 1252 | goto out; |
| 1253 | } |
| 1254 | /* |
| 1255 | * If the reparse point is unsupported by the Linux SMB |
| 1256 | * client then let it process by the SMB server. So mask |
| 1257 | * the -EOPNOTSUPP error code. This will allow Linux SMB |
| 1258 | * client to send SMB OPEN request to server. If server |
| 1259 | * does not support this reparse point too then server |
| 1260 | * will return error during open the path. |
| 1261 | */ |
| 1262 | if (rc == -EOPNOTSUPP) |
| 1263 | rc = 0; |
| 1264 | } |
| 1265 | |
| 1266 | if (data->reparse.tag == IO_REPARSE_TAG_SYMLINK && !rc) { |
| 1267 | bool directory = cifs_open_data_attrs(data) & ATTR_DIRECTORY; |
| 1268 | |
| 1269 | rc = smb2_fix_symlink_target_type(&data->symlink_target, directory, cifs_sb); |
| 1270 | } |
| 1271 | break; |
| 1272 | } |
| 1273 | |
| 1274 | if (data->contains_posix_file_info) |
| 1275 | smb311_posix_info_to_fattr(fattr, data, sb); |
| 1276 | else |
| 1277 | cifs_open_info_to_fattr(fattr, data, sb); |
| 1278 | out: |
| 1279 | fattr->cf_cifstag = data->reparse.tag; |
| 1280 | free_rsp_buf(rsp_buftype, rsp_iov.iov_base); |
| 1281 | return rc; |
| 1282 | } |
| 1283 | |
| 1284 | static int cifs_get_fattr(struct cifs_open_info_data *data, |
| 1285 | struct super_block *sb, int xid, |
| 1286 | const struct cifs_fid *fid, |
| 1287 | struct cifs_fattr *fattr, |
| 1288 | struct inode **inode, |
| 1289 | const char *full_path) |
| 1290 | { |
| 1291 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
| 1292 | struct cifs_open_info_data tmp_data = {}; |
| 1293 | void *smb1_backup_rsp_buf = NULL; |
| 1294 | struct TCP_Server_Info *server; |
| 1295 | struct cifs_tcon *tcon; |
| 1296 | struct tcon_link *tlink; |
| 1297 | unsigned int sbflags; |
| 1298 | int tmprc = 0; |
| 1299 | int rc = 0; |
| 1300 | |
| 1301 | tlink = cifs_sb_tlink(cifs_sb); |
| 1302 | if (IS_ERR(tlink)) |
| 1303 | return PTR_ERR(tlink); |
| 1304 | tcon = tlink_tcon(tlink); |
| 1305 | server = tcon->ses->server; |
| 1306 | |
| 1307 | /* |
| 1308 | * 1. Fetch file metadata if not provided (data) |
| 1309 | */ |
| 1310 | |
| 1311 | if (!data) { |
| 1312 | rc = server->ops->query_path_info(xid, tcon, cifs_sb, |
| 1313 | full_path, &tmp_data); |
| 1314 | data = &tmp_data; |
| 1315 | } |
| 1316 | |
| 1317 | /* |
| 1318 | * 2. Convert it to internal cifs metadata (fattr) |
| 1319 | */ |
| 1320 | |
| 1321 | switch (rc) { |
| 1322 | case 0: |
| 1323 | /* |
| 1324 | * If the file is a reparse point, it is more complicated |
| 1325 | * since we have to check if its reparse tag matches a known |
| 1326 | * special file type e.g. symlink or fifo or char etc. |
| 1327 | */ |
| 1328 | if (cifs_open_data_reparse(data)) { |
| 1329 | rc = reparse_info_to_fattr(data, sb, xid, tcon, |
| 1330 | full_path, fattr); |
| 1331 | } else { |
| 1332 | cifs_open_info_to_fattr(fattr, data, sb); |
| 1333 | } |
| 1334 | if (!rc && *inode && |
| 1335 | (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)) |
| 1336 | cifs_mark_open_handles_for_deleted_file(*inode, full_path); |
| 1337 | break; |
| 1338 | case -EREMOTE: |
| 1339 | /* DFS link, no metadata available on this server */ |
| 1340 | cifs_create_junction_fattr(fattr, sb); |
| 1341 | rc = 0; |
| 1342 | break; |
| 1343 | case -EACCES: |
| 1344 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 1345 | /* |
| 1346 | * perm errors, try again with backup flags if possible |
| 1347 | * |
| 1348 | * For SMB2 and later the backup intent flag |
| 1349 | * is already sent if needed on open and there |
| 1350 | * is no path based FindFirst operation to use |
| 1351 | * to retry with |
| 1352 | */ |
| 1353 | if (backup_cred(cifs_sb) && is_smb1_server(server)) { |
| 1354 | /* for easier reading */ |
| 1355 | FILE_ALL_INFO *fi; |
| 1356 | FILE_DIRECTORY_INFO *fdi; |
| 1357 | FILE_ID_FULL_DIR_INFO *si; |
| 1358 | |
| 1359 | rc = cifs_backup_query_path_info(xid, tcon, sb, |
| 1360 | full_path, |
| 1361 | &smb1_backup_rsp_buf, |
| 1362 | &fi); |
| 1363 | if (rc) |
| 1364 | goto out; |
| 1365 | |
| 1366 | move_cifs_info_to_smb2(&data->fi, fi); |
| 1367 | fdi = (FILE_DIRECTORY_INFO *)fi; |
| 1368 | si = (FILE_ID_FULL_DIR_INFO *)fi; |
| 1369 | |
| 1370 | cifs_dir_info_to_fattr(fattr, fdi, cifs_sb); |
| 1371 | fattr->cf_uniqueid = le64_to_cpu(si->UniqueId); |
| 1372 | /* uniqueid set, skip get inum step */ |
| 1373 | goto handle_mnt_opt; |
| 1374 | } else { |
| 1375 | /* nothing we can do, bail out */ |
| 1376 | goto out; |
| 1377 | } |
| 1378 | #else |
| 1379 | goto out; |
| 1380 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 1381 | break; |
| 1382 | default: |
| 1383 | cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc); |
| 1384 | goto out; |
| 1385 | } |
| 1386 | |
| 1387 | /* |
| 1388 | * 3. Get or update inode number (fattr->cf_uniqueid) |
| 1389 | */ |
| 1390 | |
| 1391 | cifs_set_fattr_ino(xid, tcon, sb, inode, full_path, data, fattr); |
| 1392 | |
| 1393 | /* |
| 1394 | * 4. Tweak fattr based on mount options |
| 1395 | */ |
| 1396 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 1397 | handle_mnt_opt: |
| 1398 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 1399 | sbflags = cifs_sb_flags(cifs_sb); |
| 1400 | /* query for SFU type info if supported and needed */ |
| 1401 | if ((fattr->cf_cifsattrs & ATTR_SYSTEM) && |
| 1402 | (sbflags & CIFS_MOUNT_UNX_EMUL)) { |
| 1403 | tmprc = cifs_sfu_type(fattr, full_path, cifs_sb, xid); |
| 1404 | if (tmprc) |
| 1405 | cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc); |
| 1406 | } |
| 1407 | |
| 1408 | /* fill in 0777 bits from ACL */ |
| 1409 | if (sbflags & CIFS_MOUNT_MODE_FROM_SID) { |
| 1410 | rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode, |
| 1411 | true, full_path, fid); |
| 1412 | if (rc == -EREMOTE) |
| 1413 | rc = 0; |
| 1414 | if (rc) { |
| 1415 | cifs_dbg(FYI, "%s: Get mode from SID failed. rc=%d\n", |
| 1416 | __func__, rc); |
| 1417 | goto out; |
| 1418 | } |
| 1419 | } else if (sbflags & CIFS_MOUNT_CIFS_ACL) { |
| 1420 | rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode, |
| 1421 | false, full_path, fid); |
| 1422 | if (rc == -EREMOTE) |
| 1423 | rc = 0; |
| 1424 | if (rc) { |
| 1425 | cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n", |
| 1426 | __func__, rc); |
| 1427 | goto out; |
| 1428 | } |
| 1429 | } else if (sbflags & CIFS_MOUNT_UNX_EMUL) |
| 1430 | /* fill in remaining high mode bits e.g. SUID, VTX */ |
| 1431 | cifs_sfu_mode(fattr, full_path, cifs_sb, xid); |
| 1432 | else if (!(tcon->posix_extensions)) |
| 1433 | /* clear write bits if ATTR_READONLY is set */ |
| 1434 | if (fattr->cf_cifsattrs & ATTR_READONLY) |
| 1435 | fattr->cf_mode &= ~(S_IWUGO); |
| 1436 | |
| 1437 | |
| 1438 | /* check for Minshall+French symlinks */ |
| 1439 | if (sbflags & CIFS_MOUNT_MF_SYMLINKS) { |
| 1440 | tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path); |
| 1441 | cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc); |
| 1442 | } |
| 1443 | |
| 1444 | out: |
| 1445 | cifs_buf_release(smb1_backup_rsp_buf); |
| 1446 | cifs_put_tlink(tlink); |
| 1447 | cifs_free_open_info(&tmp_data); |
| 1448 | return rc; |
| 1449 | } |
| 1450 | |
| 1451 | int cifs_get_inode_info(struct inode **inode, |
| 1452 | const char *full_path, |
| 1453 | struct cifs_open_info_data *data, |
| 1454 | struct super_block *sb, int xid, |
| 1455 | const struct cifs_fid *fid) |
| 1456 | { |
| 1457 | struct cifs_fattr fattr = {}; |
| 1458 | int rc; |
| 1459 | |
| 1460 | if (!data && is_inode_cache_good(*inode)) { |
| 1461 | cifs_dbg(FYI, "No need to revalidate cached inode sizes\n"); |
| 1462 | return 0; |
| 1463 | } |
| 1464 | |
| 1465 | rc = cifs_get_fattr(data, sb, xid, fid, &fattr, inode, full_path); |
| 1466 | if (rc) |
| 1467 | goto out; |
| 1468 | |
| 1469 | rc = update_inode_info(sb, &fattr, inode); |
| 1470 | out: |
| 1471 | kfree(fattr.cf_symlink_target); |
| 1472 | return rc; |
| 1473 | } |
| 1474 | |
| 1475 | static int smb311_posix_get_fattr(struct cifs_open_info_data *data, |
| 1476 | struct cifs_fattr *fattr, |
| 1477 | const char *full_path, |
| 1478 | struct super_block *sb, |
| 1479 | const unsigned int xid) |
| 1480 | { |
| 1481 | struct cifs_open_info_data tmp_data = {}; |
| 1482 | struct TCP_Server_Info *server; |
| 1483 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
| 1484 | struct cifs_tcon *tcon; |
| 1485 | struct tcon_link *tlink; |
| 1486 | int tmprc; |
| 1487 | int rc = 0; |
| 1488 | |
| 1489 | tlink = cifs_sb_tlink(cifs_sb); |
| 1490 | if (IS_ERR(tlink)) |
| 1491 | return PTR_ERR(tlink); |
| 1492 | tcon = tlink_tcon(tlink); |
| 1493 | server = tcon->ses->server; |
| 1494 | |
| 1495 | /* |
| 1496 | * 1. Fetch file metadata if not provided (data) |
| 1497 | */ |
| 1498 | if (!data) { |
| 1499 | rc = server->ops->query_path_info(xid, tcon, cifs_sb, |
| 1500 | full_path, &tmp_data); |
| 1501 | data = &tmp_data; |
| 1502 | } |
| 1503 | |
| 1504 | /* |
| 1505 | * 2. Convert it to internal cifs metadata (fattr) |
| 1506 | */ |
| 1507 | |
| 1508 | switch (rc) { |
| 1509 | case 0: |
| 1510 | if (cifs_open_data_reparse(data)) { |
| 1511 | rc = reparse_info_to_fattr(data, sb, xid, tcon, |
| 1512 | full_path, fattr); |
| 1513 | } else { |
| 1514 | smb311_posix_info_to_fattr(fattr, data, sb); |
| 1515 | } |
| 1516 | break; |
| 1517 | case -EREMOTE: |
| 1518 | /* DFS link, no metadata available on this server */ |
| 1519 | cifs_create_junction_fattr(fattr, sb); |
| 1520 | rc = 0; |
| 1521 | break; |
| 1522 | case -EACCES: |
| 1523 | /* |
| 1524 | * For SMB2 and later the backup intent flag |
| 1525 | * is already sent if needed on open and there |
| 1526 | * is no path based FindFirst operation to use |
| 1527 | * to retry with so nothing we can do, bail out |
| 1528 | */ |
| 1529 | goto out; |
| 1530 | default: |
| 1531 | cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc); |
| 1532 | goto out; |
| 1533 | } |
| 1534 | |
| 1535 | /* |
| 1536 | * 3. Tweak fattr based on mount options |
| 1537 | */ |
| 1538 | /* check for Minshall+French symlinks */ |
| 1539 | if (cifs_sb_flags(cifs_sb) & CIFS_MOUNT_MF_SYMLINKS) { |
| 1540 | tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path); |
| 1541 | cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc); |
| 1542 | } |
| 1543 | |
| 1544 | out: |
| 1545 | cifs_put_tlink(tlink); |
| 1546 | cifs_free_open_info(data); |
| 1547 | return rc; |
| 1548 | } |
| 1549 | |
| 1550 | int smb311_posix_get_inode_info(struct inode **inode, |
| 1551 | const char *full_path, |
| 1552 | struct cifs_open_info_data *data, |
| 1553 | struct super_block *sb, |
| 1554 | const unsigned int xid) |
| 1555 | { |
| 1556 | struct cifs_fattr fattr = {}; |
| 1557 | int rc; |
| 1558 | |
| 1559 | if (!data && is_inode_cache_good(*inode)) { |
| 1560 | cifs_dbg(FYI, "No need to revalidate cached inode sizes\n"); |
| 1561 | return 0; |
| 1562 | } |
| 1563 | |
| 1564 | rc = smb311_posix_get_fattr(data, &fattr, full_path, sb, xid); |
| 1565 | if (rc) |
| 1566 | goto out; |
| 1567 | |
| 1568 | rc = update_inode_info(sb, &fattr, inode); |
| 1569 | if (!rc && fattr.cf_flags & CIFS_FATTR_DELETE_PENDING) |
| 1570 | cifs_mark_open_handles_for_deleted_file(*inode, full_path); |
| 1571 | out: |
| 1572 | kfree(fattr.cf_symlink_target); |
| 1573 | return rc; |
| 1574 | } |
| 1575 | |
| 1576 | static const struct inode_operations cifs_ipc_inode_ops = { |
| 1577 | .lookup = cifs_lookup, |
| 1578 | }; |
| 1579 | |
| 1580 | static int |
| 1581 | cifs_find_inode(struct inode *inode, void *opaque) |
| 1582 | { |
| 1583 | struct cifs_fattr *fattr = opaque; |
| 1584 | |
| 1585 | /* [!] The compared values must be the same in struct cifs_fscache_inode_key. */ |
| 1586 | |
| 1587 | /* don't match inode with different uniqueid */ |
| 1588 | if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid) |
| 1589 | return 0; |
| 1590 | |
| 1591 | /* use createtime like an i_generation field */ |
| 1592 | if (CIFS_I(inode)->createtime != fattr->cf_createtime) |
| 1593 | return 0; |
| 1594 | |
| 1595 | /* don't match inode of different type */ |
| 1596 | if (inode_wrong_type(inode, fattr->cf_mode)) |
| 1597 | return 0; |
| 1598 | |
| 1599 | /* if it's not a directory or has no dentries, then flag it */ |
| 1600 | if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry)) |
| 1601 | fattr->cf_flags |= CIFS_FATTR_INO_COLLISION; |
| 1602 | |
| 1603 | return 1; |
| 1604 | } |
| 1605 | |
| 1606 | static int |
| 1607 | cifs_init_inode(struct inode *inode, void *opaque) |
| 1608 | { |
| 1609 | struct cifs_fattr *fattr = opaque; |
| 1610 | |
| 1611 | CIFS_I(inode)->uniqueid = fattr->cf_uniqueid; |
| 1612 | CIFS_I(inode)->createtime = fattr->cf_createtime; |
| 1613 | return 0; |
| 1614 | } |
| 1615 | |
| 1616 | /* |
| 1617 | * walk dentry list for an inode and report whether it has aliases that |
| 1618 | * are hashed. We use this to determine if a directory inode can actually |
| 1619 | * be used. |
| 1620 | */ |
| 1621 | static bool |
| 1622 | inode_has_hashed_dentries(struct inode *inode) |
| 1623 | { |
| 1624 | struct dentry *dentry; |
| 1625 | |
| 1626 | spin_lock(&inode->i_lock); |
| 1627 | for_each_alias(dentry, inode) { |
| 1628 | if (!d_unhashed(dentry) || IS_ROOT(dentry)) { |
| 1629 | spin_unlock(&inode->i_lock); |
| 1630 | return true; |
| 1631 | } |
| 1632 | } |
| 1633 | spin_unlock(&inode->i_lock); |
| 1634 | return false; |
| 1635 | } |
| 1636 | |
| 1637 | /* Given fattrs, get a corresponding inode */ |
| 1638 | struct inode * |
| 1639 | cifs_iget(struct super_block *sb, struct cifs_fattr *fattr) |
| 1640 | { |
| 1641 | unsigned long hash; |
| 1642 | struct inode *inode; |
| 1643 | |
| 1644 | retry_iget5_locked: |
| 1645 | cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid); |
| 1646 | |
| 1647 | /* hash down to 32-bits on 32-bit arch */ |
| 1648 | hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid); |
| 1649 | |
| 1650 | inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr); |
| 1651 | if (inode) { |
| 1652 | /* was there a potentially problematic inode collision? */ |
| 1653 | if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) { |
| 1654 | fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION; |
| 1655 | |
| 1656 | if (inode_has_hashed_dentries(inode)) { |
| 1657 | cifs_autodisable_serverino(CIFS_SB(sb), "Inode number collision detected", 0); |
| 1658 | iput(inode); |
| 1659 | fattr->cf_uniqueid = iunique(sb, ROOT_I); |
| 1660 | goto retry_iget5_locked; |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | /* can't fail - see cifs_find_inode() */ |
| 1665 | cifs_fattr_to_inode(inode, fattr, false); |
| 1666 | if (sb->s_flags & SB_NOATIME) |
| 1667 | inode->i_flags |= S_NOATIME | S_NOCMTIME; |
| 1668 | if (inode_state_read_once(inode) & I_NEW) { |
| 1669 | inode->i_ino = hash; |
| 1670 | cifs_fscache_get_inode_cookie(inode); |
| 1671 | unlock_new_inode(inode); |
| 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | return inode; |
| 1676 | } |
| 1677 | |
| 1678 | /* gets root inode */ |
| 1679 | struct inode *cifs_root_iget(struct super_block *sb) |
| 1680 | { |
| 1681 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
| 1682 | struct cifs_fattr fattr = {}; |
| 1683 | struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); |
| 1684 | struct inode *inode = NULL; |
| 1685 | unsigned int xid; |
| 1686 | char *path = NULL; |
| 1687 | int len; |
| 1688 | int rc; |
| 1689 | |
| 1690 | if ((cifs_sb_flags(cifs_sb) & CIFS_MOUNT_USE_PREFIX_PATH) |
| 1691 | && cifs_sb->prepath) { |
| 1692 | len = strlen(cifs_sb->prepath); |
| 1693 | path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL); |
| 1694 | if (path == NULL) |
| 1695 | return ERR_PTR(-ENOMEM); |
| 1696 | path[0] = '/'; |
| 1697 | memcpy(path+1, cifs_sb->prepath, len); |
| 1698 | } else { |
| 1699 | path = kstrdup("", GFP_KERNEL); |
| 1700 | if (path == NULL) |
| 1701 | return ERR_PTR(-ENOMEM); |
| 1702 | } |
| 1703 | |
| 1704 | xid = get_xid(); |
| 1705 | if (tcon->unix_ext) { |
| 1706 | rc = cifs_get_unix_fattr(path, sb, &fattr, &inode, xid); |
| 1707 | /* some servers mistakenly claim POSIX support */ |
| 1708 | if (rc != -EOPNOTSUPP) |
| 1709 | goto iget_root; |
| 1710 | cifs_dbg(VFS, "server does not support POSIX extensions\n"); |
| 1711 | tcon->unix_ext = false; |
| 1712 | } |
| 1713 | |
| 1714 | convert_delimiter(path, CIFS_DIR_SEP(cifs_sb)); |
| 1715 | if (tcon->posix_extensions) |
| 1716 | rc = smb311_posix_get_fattr(NULL, &fattr, path, sb, xid); |
| 1717 | else |
| 1718 | rc = cifs_get_fattr(NULL, sb, xid, NULL, &fattr, &inode, path); |
| 1719 | |
| 1720 | iget_root: |
| 1721 | if (!rc) { |
| 1722 | if (fattr.cf_flags & CIFS_FATTR_JUNCTION) { |
| 1723 | cifs_dbg(VFS, "Removing junction mark and disabling 'serverino' to prevent inode collisions\n"); |
| 1724 | fattr.cf_flags &= ~CIFS_FATTR_JUNCTION; |
| 1725 | cifs_autodisable_serverino(cifs_sb, "Cannot retrieve attributes for junction point", rc); |
| 1726 | } |
| 1727 | inode = cifs_iget(sb, &fattr); |
| 1728 | } |
| 1729 | |
| 1730 | if (!inode) { |
| 1731 | inode = ERR_PTR(rc); |
| 1732 | goto out; |
| 1733 | } |
| 1734 | |
| 1735 | if (!rc && fattr.cf_flags & CIFS_FATTR_DELETE_PENDING) |
| 1736 | cifs_mark_open_handles_for_deleted_file(inode, path); |
| 1737 | |
| 1738 | if (rc && tcon->pipe) { |
| 1739 | cifs_dbg(FYI, "ipc connection - fake read inode\n"); |
| 1740 | spin_lock(&inode->i_lock); |
| 1741 | inode->i_mode |= S_IFDIR; |
| 1742 | set_nlink(inode, 2); |
| 1743 | inode->i_op = &cifs_ipc_inode_ops; |
| 1744 | inode->i_fop = &simple_dir_operations; |
| 1745 | inode->i_uid = cifs_sb->ctx->linux_uid; |
| 1746 | inode->i_gid = cifs_sb->ctx->linux_gid; |
| 1747 | spin_unlock(&inode->i_lock); |
| 1748 | } else if (rc) { |
| 1749 | iget_failed(inode); |
| 1750 | inode = ERR_PTR(rc); |
| 1751 | } |
| 1752 | |
| 1753 | out: |
| 1754 | kfree(path); |
| 1755 | free_xid(xid); |
| 1756 | kfree(fattr.cf_symlink_target); |
| 1757 | return inode; |
| 1758 | } |
| 1759 | |
| 1760 | int |
| 1761 | cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid, |
| 1762 | const char *full_path, __u32 dosattr) |
| 1763 | { |
| 1764 | bool set_time = false; |
| 1765 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 1766 | struct TCP_Server_Info *server; |
| 1767 | FILE_BASIC_INFO info_buf; |
| 1768 | |
| 1769 | if (attrs == NULL) |
| 1770 | return -EINVAL; |
| 1771 | |
| 1772 | server = cifs_sb_master_tcon(cifs_sb)->ses->server; |
| 1773 | if (!server->ops->set_file_info) |
| 1774 | return -ENOSYS; |
| 1775 | |
| 1776 | info_buf.Pad = 0; |
| 1777 | |
| 1778 | if (attrs->ia_valid & ATTR_ATIME) { |
| 1779 | set_time = true; |
| 1780 | info_buf.LastAccessTime = |
| 1781 | cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime)); |
| 1782 | } else |
| 1783 | info_buf.LastAccessTime = 0; |
| 1784 | |
| 1785 | if (attrs->ia_valid & ATTR_MTIME) { |
| 1786 | set_time = true; |
| 1787 | info_buf.LastWriteTime = |
| 1788 | cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime)); |
| 1789 | } else |
| 1790 | info_buf.LastWriteTime = 0; |
| 1791 | |
| 1792 | /* |
| 1793 | * Samba throws this field away, but windows may actually use it. |
| 1794 | * Do not set ctime unless other time stamps are changed explicitly |
| 1795 | * (i.e. by utimes()) since we would then have a mix of client and |
| 1796 | * server times. |
| 1797 | */ |
| 1798 | if (set_time && (attrs->ia_valid & ATTR_CTIME)) { |
| 1799 | cifs_dbg(FYI, "CIFS - CTIME changed\n"); |
| 1800 | info_buf.ChangeTime = |
| 1801 | cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime)); |
| 1802 | } else |
| 1803 | info_buf.ChangeTime = 0; |
| 1804 | |
| 1805 | info_buf.CreationTime = 0; /* don't change */ |
| 1806 | info_buf.Attributes = cpu_to_le32(dosattr); |
| 1807 | |
| 1808 | return server->ops->set_file_info(inode, full_path, &info_buf, xid); |
| 1809 | } |
| 1810 | |
| 1811 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 1812 | /* |
| 1813 | * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit |
| 1814 | * and rename it to a random name that hopefully won't conflict with |
| 1815 | * anything else. |
| 1816 | */ |
| 1817 | int |
| 1818 | cifs_rename_pending_delete(const char *full_path, struct dentry *dentry, |
| 1819 | const unsigned int xid) |
| 1820 | { |
| 1821 | int oplock = 0; |
| 1822 | int rc; |
| 1823 | struct cifs_fid fid; |
| 1824 | struct cifs_open_parms oparms; |
| 1825 | struct inode *inode = d_inode(dentry); |
| 1826 | struct cifsInodeInfo *cifsInode = CIFS_I(inode); |
| 1827 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 1828 | struct tcon_link *tlink; |
| 1829 | struct cifs_tcon *tcon; |
| 1830 | __u32 dosattr, origattr; |
| 1831 | FILE_BASIC_INFO *info_buf = NULL; |
| 1832 | |
| 1833 | tlink = cifs_sb_tlink(cifs_sb); |
| 1834 | if (IS_ERR(tlink)) |
| 1835 | return PTR_ERR(tlink); |
| 1836 | tcon = tlink_tcon(tlink); |
| 1837 | |
| 1838 | /* |
| 1839 | * We cannot rename the file if the server doesn't support |
| 1840 | * CAP_INFOLEVEL_PASSTHRU |
| 1841 | */ |
| 1842 | if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) { |
| 1843 | rc = -EBUSY; |
| 1844 | goto out; |
| 1845 | } |
| 1846 | |
| 1847 | oparms = (struct cifs_open_parms) { |
| 1848 | .tcon = tcon, |
| 1849 | .cifs_sb = cifs_sb, |
| 1850 | .desired_access = DELETE | FILE_WRITE_ATTRIBUTES, |
| 1851 | .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR), |
| 1852 | .disposition = FILE_OPEN, |
| 1853 | .path = full_path, |
| 1854 | .fid = &fid, |
| 1855 | }; |
| 1856 | |
| 1857 | rc = CIFS_open(xid, &oparms, &oplock, NULL); |
| 1858 | if (rc != 0) |
| 1859 | goto out; |
| 1860 | |
| 1861 | origattr = cifsInode->cifsAttrs; |
| 1862 | if (origattr == 0) |
| 1863 | origattr |= ATTR_NORMAL; |
| 1864 | |
| 1865 | dosattr = origattr & ~ATTR_READONLY; |
| 1866 | if (dosattr == 0) |
| 1867 | dosattr |= ATTR_NORMAL; |
| 1868 | dosattr |= ATTR_HIDDEN; |
| 1869 | |
| 1870 | /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */ |
| 1871 | if (dosattr != origattr) { |
| 1872 | info_buf = kzalloc_obj(*info_buf); |
| 1873 | if (info_buf == NULL) { |
| 1874 | rc = -ENOMEM; |
| 1875 | goto out_close; |
| 1876 | } |
| 1877 | info_buf->Attributes = cpu_to_le32(dosattr); |
| 1878 | rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid, |
| 1879 | current->tgid); |
| 1880 | /* although we would like to mark the file hidden |
| 1881 | if that fails we will still try to rename it */ |
| 1882 | if (!rc) |
| 1883 | cifsInode->cifsAttrs = dosattr; |
| 1884 | else |
| 1885 | dosattr = origattr; /* since not able to change them */ |
| 1886 | } |
| 1887 | |
| 1888 | /* rename the file */ |
| 1889 | rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL, |
| 1890 | cifs_sb->local_nls, |
| 1891 | cifs_remap(cifs_sb)); |
| 1892 | if (rc != 0) { |
| 1893 | rc = -EBUSY; |
| 1894 | goto undo_setattr; |
| 1895 | } |
| 1896 | |
| 1897 | /* try to set DELETE_ON_CLOSE */ |
| 1898 | if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) { |
| 1899 | rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid, |
| 1900 | current->tgid); |
| 1901 | /* |
| 1902 | * some samba versions return -ENOENT when we try to set the |
| 1903 | * file disposition here. Likely a samba bug, but work around |
| 1904 | * it for now. This means that some cifsXXX files may hang |
| 1905 | * around after they shouldn't. |
| 1906 | * |
| 1907 | * BB: remove this hack after more servers have the fix |
| 1908 | */ |
| 1909 | if (rc == -ENOENT) |
| 1910 | rc = 0; |
| 1911 | else if (rc != 0) { |
| 1912 | rc = -EBUSY; |
| 1913 | goto undo_rename; |
| 1914 | } |
| 1915 | set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags); |
| 1916 | } |
| 1917 | |
| 1918 | out_close: |
| 1919 | CIFSSMBClose(xid, tcon, fid.netfid); |
| 1920 | out: |
| 1921 | kfree(info_buf); |
| 1922 | cifs_put_tlink(tlink); |
| 1923 | return rc; |
| 1924 | |
| 1925 | /* |
| 1926 | * reset everything back to the original state. Don't bother |
| 1927 | * dealing with errors here since we can't do anything about |
| 1928 | * them anyway. |
| 1929 | */ |
| 1930 | undo_rename: |
| 1931 | CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name, |
| 1932 | cifs_sb->local_nls, cifs_remap(cifs_sb)); |
| 1933 | undo_setattr: |
| 1934 | if (dosattr != origattr) { |
| 1935 | info_buf->Attributes = cpu_to_le32(origattr); |
| 1936 | if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid, |
| 1937 | current->tgid)) |
| 1938 | cifsInode->cifsAttrs = origattr; |
| 1939 | } |
| 1940 | |
| 1941 | goto out_close; |
| 1942 | } |
| 1943 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 1944 | |
| 1945 | /* copied from fs/nfs/dir.c with small changes */ |
| 1946 | static void |
| 1947 | cifs_drop_nlink(struct inode *inode) |
| 1948 | { |
| 1949 | spin_lock(&inode->i_lock); |
| 1950 | if (inode->i_nlink > 0) |
| 1951 | drop_nlink(inode); |
| 1952 | spin_unlock(&inode->i_lock); |
| 1953 | } |
| 1954 | |
| 1955 | /* |
| 1956 | * If d_inode(dentry) is null (usually meaning the cached dentry |
| 1957 | * is a negative dentry) then we would attempt a standard SMB delete, but |
| 1958 | * if that fails we can not attempt the fall back mechanisms on EACCES |
| 1959 | * but will return the EACCES to the caller. Note that the VFS does not call |
| 1960 | * unlink on negative dentries currently. |
| 1961 | */ |
| 1962 | static int __cifs_unlink(struct inode *dir, struct dentry *dentry, bool sillyrename) |
| 1963 | { |
| 1964 | int rc = 0; |
| 1965 | unsigned int xid; |
| 1966 | const char *full_path; |
| 1967 | void *page; |
| 1968 | struct inode *inode = d_inode(dentry); |
| 1969 | struct cifsInodeInfo *cifs_inode; |
| 1970 | struct super_block *sb = dir->i_sb; |
| 1971 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
| 1972 | struct tcon_link *tlink; |
| 1973 | struct cifs_tcon *tcon; |
| 1974 | __u32 dosattr = 0, origattr = 0; |
| 1975 | struct TCP_Server_Info *server; |
| 1976 | struct iattr *attrs = NULL; |
| 1977 | bool rehash = false; |
| 1978 | |
| 1979 | cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry); |
| 1980 | |
| 1981 | if (unlikely(cifs_forced_shutdown(cifs_sb))) |
| 1982 | return smb_EIO(smb_eio_trace_forced_shutdown); |
| 1983 | |
| 1984 | /* Unhash dentry in advance to prevent any concurrent opens */ |
| 1985 | spin_lock(&dentry->d_lock); |
| 1986 | if (!d_unhashed(dentry)) { |
| 1987 | __d_drop(dentry); |
| 1988 | rehash = true; |
| 1989 | } |
| 1990 | spin_unlock(&dentry->d_lock); |
| 1991 | |
| 1992 | tlink = cifs_sb_tlink(cifs_sb); |
| 1993 | if (IS_ERR(tlink)) |
| 1994 | return PTR_ERR(tlink); |
| 1995 | tcon = tlink_tcon(tlink); |
| 1996 | server = tcon->ses->server; |
| 1997 | |
| 1998 | xid = get_xid(); |
| 1999 | page = alloc_dentry_path(); |
| 2000 | |
| 2001 | if (tcon->nodelete) { |
| 2002 | rc = -EACCES; |
| 2003 | goto unlink_out; |
| 2004 | } |
| 2005 | |
| 2006 | /* Unlink can be called from rename so we can not take the |
| 2007 | * sb->s_vfs_rename_mutex here */ |
| 2008 | full_path = build_path_from_dentry(dentry, page); |
| 2009 | if (IS_ERR(full_path)) { |
| 2010 | rc = PTR_ERR(full_path); |
| 2011 | goto unlink_out; |
| 2012 | } |
| 2013 | |
| 2014 | netfs_wait_for_outstanding_io(inode); |
| 2015 | cifs_close_deferred_file_under_dentry(tcon, dentry); |
| 2016 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 2017 | if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
| 2018 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { |
| 2019 | rc = CIFSPOSIXDelFile(xid, tcon, full_path, |
| 2020 | SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls, |
| 2021 | cifs_remap(cifs_sb)); |
| 2022 | cifs_dbg(FYI, "posix del rc %d\n", rc); |
| 2023 | if ((rc == 0) || (rc == -ENOENT)) |
| 2024 | goto psx_del_no_retry; |
| 2025 | } |
| 2026 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 2027 | |
| 2028 | retry_std_delete: |
| 2029 | if (!server->ops->unlink) { |
| 2030 | rc = -ENOSYS; |
| 2031 | goto psx_del_no_retry; |
| 2032 | } |
| 2033 | |
| 2034 | /* For SMB2+, if the file is open, we always perform a silly rename. |
| 2035 | * |
| 2036 | * We check for d_count() right after calling |
| 2037 | * cifs_close_deferred_file_under_dentry() to make sure that the |
| 2038 | * dentry's refcount gets dropped in case the file had any deferred |
| 2039 | * close. |
| 2040 | */ |
| 2041 | if (!sillyrename && server->vals->protocol_id > SMB10_PROT_ID) { |
| 2042 | spin_lock(&dentry->d_lock); |
| 2043 | if (d_count(dentry) > 1) |
| 2044 | sillyrename = true; |
| 2045 | spin_unlock(&dentry->d_lock); |
| 2046 | } |
| 2047 | |
| 2048 | if (sillyrename) |
| 2049 | rc = -EBUSY; |
| 2050 | else |
| 2051 | rc = server->ops->unlink(xid, tcon, full_path, cifs_sb, dentry); |
| 2052 | |
| 2053 | psx_del_no_retry: |
| 2054 | if (!rc) { |
| 2055 | if (inode) { |
| 2056 | cifs_mark_open_handles_for_deleted_file(inode, full_path); |
| 2057 | cifs_drop_nlink(inode); |
| 2058 | } |
| 2059 | } else if (rc == -ENOENT) { |
| 2060 | if (simple_positive(dentry)) |
| 2061 | d_delete(dentry); |
| 2062 | } else if (rc == -EBUSY) { |
| 2063 | if (server->ops->rename_pending_delete) { |
| 2064 | rc = server->ops->rename_pending_delete(full_path, |
| 2065 | dentry, xid); |
| 2066 | if (rc == 0) { |
| 2067 | cifs_mark_open_handles_for_deleted_file(inode, full_path); |
| 2068 | cifs_drop_nlink(inode); |
| 2069 | } |
| 2070 | } |
| 2071 | } else if ((rc == -EACCES) && (dosattr == 0) && inode) { |
| 2072 | attrs = kzalloc_obj(*attrs); |
| 2073 | if (attrs == NULL) { |
| 2074 | rc = -ENOMEM; |
| 2075 | goto out_reval; |
| 2076 | } |
| 2077 | |
| 2078 | /* try to reset dos attributes */ |
| 2079 | cifs_inode = CIFS_I(inode); |
| 2080 | origattr = cifs_inode->cifsAttrs; |
| 2081 | if (origattr == 0) |
| 2082 | origattr |= ATTR_NORMAL; |
| 2083 | dosattr = origattr & ~ATTR_READONLY; |
| 2084 | if (dosattr == 0) |
| 2085 | dosattr |= ATTR_NORMAL; |
| 2086 | dosattr |= ATTR_HIDDEN; |
| 2087 | |
| 2088 | rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr); |
| 2089 | if (rc != 0) |
| 2090 | goto out_reval; |
| 2091 | |
| 2092 | goto retry_std_delete; |
| 2093 | } |
| 2094 | |
| 2095 | /* undo the setattr if we errored out and it's needed */ |
| 2096 | if (rc != 0 && dosattr != 0) |
| 2097 | cifs_set_file_info(inode, attrs, xid, full_path, origattr); |
| 2098 | |
| 2099 | out_reval: |
| 2100 | if (!rc && dentry->d_parent) |
| 2101 | cifs_invalidate_cached_dir(tcon, dentry->d_parent); |
| 2102 | |
| 2103 | if (inode) { |
| 2104 | cifs_inode = CIFS_I(inode); |
| 2105 | cifs_inode->time = 0; /* will force revalidate to get info |
| 2106 | when needed */ |
| 2107 | inode_set_ctime_current(inode); |
| 2108 | } |
| 2109 | inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); |
| 2110 | cifs_inode = CIFS_I(dir); |
| 2111 | CIFS_I(dir)->time = 0; /* force revalidate of dir as well */ |
| 2112 | unlink_out: |
| 2113 | free_dentry_path(page); |
| 2114 | kfree(attrs); |
| 2115 | free_xid(xid); |
| 2116 | cifs_put_tlink(tlink); |
| 2117 | if (rehash) |
| 2118 | d_rehash(dentry); |
| 2119 | return rc; |
| 2120 | } |
| 2121 | |
| 2122 | int cifs_unlink(struct inode *dir, struct dentry *dentry) |
| 2123 | { |
| 2124 | return __cifs_unlink(dir, dentry, false); |
| 2125 | } |
| 2126 | |
| 2127 | static int |
| 2128 | cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode, |
| 2129 | const char *full_path, struct cifs_sb_info *cifs_sb, |
| 2130 | struct cifs_tcon *tcon, const unsigned int xid) |
| 2131 | { |
| 2132 | struct inode *inode = NULL; |
| 2133 | unsigned int sbflags; |
| 2134 | int rc = 0; |
| 2135 | |
| 2136 | if (tcon->posix_extensions) { |
| 2137 | rc = smb311_posix_get_inode_info(&inode, full_path, |
| 2138 | NULL, parent->i_sb, xid); |
| 2139 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 2140 | } else if (tcon->unix_ext) { |
| 2141 | rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb, |
| 2142 | xid); |
| 2143 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 2144 | } else { |
| 2145 | rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb, |
| 2146 | xid, NULL); |
| 2147 | } |
| 2148 | |
| 2149 | if (rc) |
| 2150 | return rc; |
| 2151 | |
| 2152 | if (!S_ISDIR(inode->i_mode)) { |
| 2153 | /* |
| 2154 | * mkdir succeeded, but another client has managed to remove the |
| 2155 | * sucker and replace it with non-directory. Return success, |
| 2156 | * but don't leave the child in dcache. |
| 2157 | */ |
| 2158 | iput(inode); |
| 2159 | d_drop(dentry); |
| 2160 | return 0; |
| 2161 | } |
| 2162 | /* |
| 2163 | * setting nlink not necessary except in cases where we failed to get it |
| 2164 | * from the server or was set bogus. Also, since this is a brand new |
| 2165 | * inode, no need to grab the i_lock before setting the i_nlink. |
| 2166 | */ |
| 2167 | if (inode->i_nlink < 2) |
| 2168 | set_nlink(inode, 2); |
| 2169 | mode &= ~current_umask(); |
| 2170 | /* must turn on setgid bit if parent dir has it */ |
| 2171 | if (parent->i_mode & S_ISGID) |
| 2172 | mode |= S_ISGID; |
| 2173 | |
| 2174 | sbflags = cifs_sb_flags(cifs_sb); |
| 2175 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 2176 | if (tcon->unix_ext) { |
| 2177 | struct cifs_unix_set_info_args args = { |
| 2178 | .mode = mode, |
| 2179 | .ctime = NO_CHANGE_64, |
| 2180 | .atime = NO_CHANGE_64, |
| 2181 | .mtime = NO_CHANGE_64, |
| 2182 | .device = 0, |
| 2183 | }; |
| 2184 | if (sbflags & CIFS_MOUNT_SET_UID) { |
| 2185 | args.uid = current_fsuid(); |
| 2186 | if (parent->i_mode & S_ISGID) |
| 2187 | args.gid = parent->i_gid; |
| 2188 | else |
| 2189 | args.gid = current_fsgid(); |
| 2190 | } else { |
| 2191 | args.uid = INVALID_UID; /* no change */ |
| 2192 | args.gid = INVALID_GID; /* no change */ |
| 2193 | } |
| 2194 | CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args, |
| 2195 | cifs_sb->local_nls, |
| 2196 | cifs_remap(cifs_sb)); |
| 2197 | } else { |
| 2198 | #else |
| 2199 | { |
| 2200 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 2201 | struct TCP_Server_Info *server = tcon->ses->server; |
| 2202 | if (!(sbflags & CIFS_MOUNT_CIFS_ACL) && |
| 2203 | (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo) |
| 2204 | server->ops->mkdir_setinfo(inode, full_path, cifs_sb, |
| 2205 | tcon, xid); |
| 2206 | if (sbflags & CIFS_MOUNT_DYNPERM) |
| 2207 | inode->i_mode = (mode | S_IFDIR); |
| 2208 | |
| 2209 | if (sbflags & CIFS_MOUNT_SET_UID) { |
| 2210 | inode->i_uid = current_fsuid(); |
| 2211 | if (inode->i_mode & S_ISGID) |
| 2212 | inode->i_gid = parent->i_gid; |
| 2213 | else |
| 2214 | inode->i_gid = current_fsgid(); |
| 2215 | } |
| 2216 | } |
| 2217 | d_instantiate(dentry, inode); |
| 2218 | return 0; |
| 2219 | } |
| 2220 | |
| 2221 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 2222 | static int |
| 2223 | cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode, |
| 2224 | const char *full_path, struct cifs_sb_info *cifs_sb, |
| 2225 | struct cifs_tcon *tcon, const unsigned int xid) |
| 2226 | { |
| 2227 | int rc = 0; |
| 2228 | u32 oplock = 0; |
| 2229 | FILE_UNIX_BASIC_INFO *info = NULL; |
| 2230 | struct inode *newinode = NULL; |
| 2231 | struct cifs_fattr fattr; |
| 2232 | |
| 2233 | info = kzalloc_obj(FILE_UNIX_BASIC_INFO); |
| 2234 | if (info == NULL) { |
| 2235 | rc = -ENOMEM; |
| 2236 | goto posix_mkdir_out; |
| 2237 | } |
| 2238 | |
| 2239 | mode &= ~current_umask(); |
| 2240 | rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode, |
| 2241 | NULL /* netfid */, info, &oplock, full_path, |
| 2242 | cifs_sb->local_nls, cifs_remap(cifs_sb)); |
| 2243 | if (rc == -EOPNOTSUPP) |
| 2244 | goto posix_mkdir_out; |
| 2245 | else if (rc) { |
| 2246 | cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc); |
| 2247 | d_drop(dentry); |
| 2248 | goto posix_mkdir_out; |
| 2249 | } |
| 2250 | |
| 2251 | if (info->Type == cpu_to_le32(-1)) |
| 2252 | /* no return info, go query for it */ |
| 2253 | goto posix_mkdir_get_info; |
| 2254 | /* |
| 2255 | * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if |
| 2256 | * need to set uid/gid. |
| 2257 | */ |
| 2258 | |
| 2259 | cifs_unix_basic_to_fattr(&fattr, info, cifs_sb); |
| 2260 | cifs_fill_uniqueid(inode->i_sb, &fattr); |
| 2261 | newinode = cifs_iget(inode->i_sb, &fattr); |
| 2262 | if (!newinode) |
| 2263 | goto posix_mkdir_get_info; |
| 2264 | |
| 2265 | d_instantiate(dentry, newinode); |
| 2266 | |
| 2267 | #ifdef CONFIG_CIFS_DEBUG2 |
| 2268 | cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n", |
| 2269 | dentry, dentry, newinode); |
| 2270 | |
| 2271 | if (newinode->i_nlink != 2) |
| 2272 | cifs_dbg(FYI, "unexpected number of links %d\n", |
| 2273 | newinode->i_nlink); |
| 2274 | #endif |
| 2275 | |
| 2276 | posix_mkdir_out: |
| 2277 | kfree(info); |
| 2278 | return rc; |
| 2279 | posix_mkdir_get_info: |
| 2280 | rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon, |
| 2281 | xid); |
| 2282 | goto posix_mkdir_out; |
| 2283 | } |
| 2284 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 2285 | |
| 2286 | struct dentry *cifs_mkdir(struct mnt_idmap *idmap, struct inode *inode, |
| 2287 | struct dentry *direntry, umode_t mode) |
| 2288 | { |
| 2289 | int rc = 0; |
| 2290 | unsigned int xid; |
| 2291 | struct cifs_sb_info *cifs_sb; |
| 2292 | struct tcon_link *tlink; |
| 2293 | struct cifs_tcon *tcon; |
| 2294 | struct TCP_Server_Info *server; |
| 2295 | const char *full_path; |
| 2296 | void *page; |
| 2297 | |
| 2298 | /* |
| 2299 | * vfs_mkdir() now passes S_IFDIR in @mode, but @mode is forwarded |
| 2300 | * verbatim to the server and in the past only contained permission |
| 2301 | * bits. Strip the type bit until SMB is verified to deal with it. |
| 2302 | */ |
| 2303 | mode &= ~S_IFDIR; |
| 2304 | |
| 2305 | cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n", |
| 2306 | mode, inode); |
| 2307 | |
| 2308 | cifs_sb = CIFS_SB(inode->i_sb); |
| 2309 | if (unlikely(cifs_forced_shutdown(cifs_sb))) |
| 2310 | return ERR_PTR(smb_EIO(smb_eio_trace_forced_shutdown)); |
| 2311 | tlink = cifs_sb_tlink(cifs_sb); |
| 2312 | if (IS_ERR(tlink)) |
| 2313 | return ERR_CAST(tlink); |
| 2314 | tcon = tlink_tcon(tlink); |
| 2315 | |
| 2316 | xid = get_xid(); |
| 2317 | |
| 2318 | page = alloc_dentry_path(); |
| 2319 | full_path = build_path_from_dentry(direntry, page); |
| 2320 | if (IS_ERR(full_path)) { |
| 2321 | rc = PTR_ERR(full_path); |
| 2322 | goto mkdir_out; |
| 2323 | } |
| 2324 | |
| 2325 | server = tcon->ses->server; |
| 2326 | |
| 2327 | if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) { |
| 2328 | rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path, |
| 2329 | cifs_sb); |
| 2330 | d_drop(direntry); /* for time being always refresh inode info */ |
| 2331 | goto mkdir_out; |
| 2332 | } |
| 2333 | |
| 2334 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 2335 | if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
| 2336 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { |
| 2337 | rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb, |
| 2338 | tcon, xid); |
| 2339 | if (rc != -EOPNOTSUPP) |
| 2340 | goto mkdir_out; |
| 2341 | } |
| 2342 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 2343 | |
| 2344 | if (!server->ops->mkdir) { |
| 2345 | rc = -ENOSYS; |
| 2346 | goto mkdir_out; |
| 2347 | } |
| 2348 | |
| 2349 | /* BB add setting the equivalent of mode via CreateX w/ACLs */ |
| 2350 | rc = server->ops->mkdir(xid, inode, mode, tcon, full_path, cifs_sb); |
| 2351 | if (rc) { |
| 2352 | cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc); |
| 2353 | d_drop(direntry); |
| 2354 | goto mkdir_out; |
| 2355 | } |
| 2356 | |
| 2357 | /* TODO: skip this for smb2/smb3 */ |
| 2358 | rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon, |
| 2359 | xid); |
| 2360 | mkdir_out: |
| 2361 | /* |
| 2362 | * Force revalidate to get parent dir info when needed since cached |
| 2363 | * attributes are invalid now. |
| 2364 | */ |
| 2365 | CIFS_I(inode)->time = 0; |
| 2366 | free_dentry_path(page); |
| 2367 | free_xid(xid); |
| 2368 | cifs_put_tlink(tlink); |
| 2369 | return ERR_PTR(rc); |
| 2370 | } |
| 2371 | |
| 2372 | int cifs_rmdir(struct inode *inode, struct dentry *direntry) |
| 2373 | { |
| 2374 | int rc = 0; |
| 2375 | unsigned int xid; |
| 2376 | struct cifs_sb_info *cifs_sb; |
| 2377 | struct tcon_link *tlink; |
| 2378 | struct cifs_tcon *tcon; |
| 2379 | struct TCP_Server_Info *server; |
| 2380 | const char *full_path; |
| 2381 | void *page = alloc_dentry_path(); |
| 2382 | struct cifsInodeInfo *cifsInode; |
| 2383 | |
| 2384 | cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode); |
| 2385 | |
| 2386 | xid = get_xid(); |
| 2387 | |
| 2388 | full_path = build_path_from_dentry(direntry, page); |
| 2389 | if (IS_ERR(full_path)) { |
| 2390 | rc = PTR_ERR(full_path); |
| 2391 | goto rmdir_exit; |
| 2392 | } |
| 2393 | |
| 2394 | cifs_sb = CIFS_SB(inode->i_sb); |
| 2395 | if (unlikely(cifs_forced_shutdown(cifs_sb))) { |
| 2396 | rc = smb_EIO(smb_eio_trace_forced_shutdown); |
| 2397 | goto rmdir_exit; |
| 2398 | } |
| 2399 | |
| 2400 | tlink = cifs_sb_tlink(cifs_sb); |
| 2401 | if (IS_ERR(tlink)) { |
| 2402 | rc = PTR_ERR(tlink); |
| 2403 | goto rmdir_exit; |
| 2404 | } |
| 2405 | tcon = tlink_tcon(tlink); |
| 2406 | server = tcon->ses->server; |
| 2407 | |
| 2408 | if (!server->ops->rmdir) { |
| 2409 | rc = -ENOSYS; |
| 2410 | cifs_put_tlink(tlink); |
| 2411 | goto rmdir_exit; |
| 2412 | } |
| 2413 | |
| 2414 | if (tcon->nodelete) { |
| 2415 | rc = -EACCES; |
| 2416 | cifs_put_tlink(tlink); |
| 2417 | goto rmdir_exit; |
| 2418 | } |
| 2419 | |
| 2420 | rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb); |
| 2421 | |
| 2422 | cifsInode = CIFS_I(d_inode(direntry)); |
| 2423 | |
| 2424 | if (!rc) { |
| 2425 | set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags); |
| 2426 | spin_lock(&d_inode(direntry)->i_lock); |
| 2427 | i_size_write(d_inode(direntry), 0); |
| 2428 | clear_nlink(d_inode(direntry)); |
| 2429 | spin_unlock(&d_inode(direntry)->i_lock); |
| 2430 | if (direntry->d_parent) |
| 2431 | cifs_invalidate_cached_dir(tcon, direntry->d_parent); |
| 2432 | } |
| 2433 | |
| 2434 | /* force revalidate to go get info when needed */ |
| 2435 | cifsInode->time = 0; |
| 2436 | |
| 2437 | cifsInode = CIFS_I(inode); |
| 2438 | /* |
| 2439 | * Force revalidate to get parent dir info when needed since cached |
| 2440 | * attributes are invalid now. |
| 2441 | */ |
| 2442 | cifsInode->time = 0; |
| 2443 | |
| 2444 | inode_set_ctime_current(d_inode(direntry)); |
| 2445 | inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); |
| 2446 | cifs_put_tlink(tlink); |
| 2447 | |
| 2448 | rmdir_exit: |
| 2449 | free_dentry_path(page); |
| 2450 | free_xid(xid); |
| 2451 | return rc; |
| 2452 | } |
| 2453 | |
| 2454 | static int |
| 2455 | cifs_do_rename(const unsigned int xid, struct dentry *from_dentry, |
| 2456 | const char *from_path, struct dentry *to_dentry, |
| 2457 | const char *to_path) |
| 2458 | { |
| 2459 | struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb); |
| 2460 | struct tcon_link *tlink; |
| 2461 | struct cifs_tcon *tcon; |
| 2462 | struct TCP_Server_Info *server; |
| 2463 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 2464 | struct cifs_fid fid; |
| 2465 | struct cifs_open_parms oparms; |
| 2466 | int oplock; |
| 2467 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 2468 | int rc; |
| 2469 | |
| 2470 | tlink = cifs_sb_tlink(cifs_sb); |
| 2471 | if (IS_ERR(tlink)) |
| 2472 | return PTR_ERR(tlink); |
| 2473 | tcon = tlink_tcon(tlink); |
| 2474 | server = tcon->ses->server; |
| 2475 | |
| 2476 | if (!server->ops->rename) { |
| 2477 | rc = -ENOSYS; |
| 2478 | goto do_rename_exit; |
| 2479 | } |
| 2480 | |
| 2481 | /* try path-based rename first */ |
| 2482 | rc = server->ops->rename(xid, tcon, from_dentry, |
| 2483 | from_path, to_path, cifs_sb); |
| 2484 | |
| 2485 | /* |
| 2486 | * Don't bother with rename by filehandle unless file is busy and |
| 2487 | * source. Note that cross directory moves do not work with |
| 2488 | * rename by filehandle to various Windows servers. |
| 2489 | */ |
| 2490 | if (rc == 0 || rc != -EBUSY) |
| 2491 | goto do_rename_exit; |
| 2492 | |
| 2493 | /* Don't fall back to using SMB on SMB 2+ mount */ |
| 2494 | if (server->vals->protocol_id != 0) |
| 2495 | goto do_rename_exit; |
| 2496 | |
| 2497 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 2498 | /* open-file renames don't work across directories */ |
| 2499 | if (to_dentry->d_parent != from_dentry->d_parent) |
| 2500 | goto do_rename_exit; |
| 2501 | |
| 2502 | /* |
| 2503 | * CIFSSMBRenameOpenFile() uses SMB_SET_FILE_RENAME_INFORMATION |
| 2504 | * which is SMB PASSTHROUGH level. |
| 2505 | */ |
| 2506 | if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) |
| 2507 | goto do_rename_exit; |
| 2508 | |
| 2509 | oparms = (struct cifs_open_parms) { |
| 2510 | .tcon = tcon, |
| 2511 | .cifs_sb = cifs_sb, |
| 2512 | /* open the file to be renamed -- we need DELETE perms */ |
| 2513 | .desired_access = DELETE, |
| 2514 | .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR), |
| 2515 | .disposition = FILE_OPEN, |
| 2516 | .path = from_path, |
| 2517 | .fid = &fid, |
| 2518 | }; |
| 2519 | |
| 2520 | rc = CIFS_open(xid, &oparms, &oplock, NULL); |
| 2521 | if (rc == 0) { |
| 2522 | rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, |
| 2523 | (const char *) to_dentry->d_name.name, |
| 2524 | cifs_sb->local_nls, cifs_remap(cifs_sb)); |
| 2525 | CIFSSMBClose(xid, tcon, fid.netfid); |
| 2526 | } |
| 2527 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 2528 | do_rename_exit: |
| 2529 | if (rc == 0) |
| 2530 | d_move(from_dentry, to_dentry); |
| 2531 | cifs_put_tlink(tlink); |
| 2532 | return rc; |
| 2533 | } |
| 2534 | |
| 2535 | int |
| 2536 | cifs_rename2(struct mnt_idmap *idmap, struct inode *source_dir, |
| 2537 | struct dentry *source_dentry, struct inode *target_dir, |
| 2538 | struct dentry *target_dentry, unsigned int flags) |
| 2539 | { |
| 2540 | const char *from_name, *to_name; |
| 2541 | struct TCP_Server_Info *server; |
| 2542 | void *page1, *page2; |
| 2543 | struct cifs_sb_info *cifs_sb; |
| 2544 | struct tcon_link *tlink; |
| 2545 | struct cifs_tcon *tcon; |
| 2546 | bool rehash = false; |
| 2547 | unsigned int xid; |
| 2548 | int rc, tmprc; |
| 2549 | int retry_count = 0; |
| 2550 | FILE_UNIX_BASIC_INFO *info_buf_source = NULL; |
| 2551 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 2552 | FILE_UNIX_BASIC_INFO *info_buf_target; |
| 2553 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 2554 | |
| 2555 | if (flags & ~RENAME_NOREPLACE) |
| 2556 | return -EINVAL; |
| 2557 | |
| 2558 | cifs_sb = CIFS_SB(source_dir->i_sb); |
| 2559 | if (unlikely(cifs_forced_shutdown(cifs_sb))) |
| 2560 | return smb_EIO(smb_eio_trace_forced_shutdown); |
| 2561 | |
| 2562 | /* |
| 2563 | * Prevent any concurrent opens on the target by unhashing the dentry. |
| 2564 | * VFS already unhashes the target when renaming directories. |
| 2565 | */ |
| 2566 | if (d_is_positive(target_dentry) && !d_is_dir(target_dentry)) { |
| 2567 | if (!d_unhashed(target_dentry)) { |
| 2568 | d_drop(target_dentry); |
| 2569 | rehash = true; |
| 2570 | } |
| 2571 | } |
| 2572 | |
| 2573 | tlink = cifs_sb_tlink(cifs_sb); |
| 2574 | if (IS_ERR(tlink)) |
| 2575 | return PTR_ERR(tlink); |
| 2576 | tcon = tlink_tcon(tlink); |
| 2577 | server = tcon->ses->server; |
| 2578 | |
| 2579 | page1 = alloc_dentry_path(); |
| 2580 | page2 = alloc_dentry_path(); |
| 2581 | xid = get_xid(); |
| 2582 | |
| 2583 | from_name = build_path_from_dentry(source_dentry, page1); |
| 2584 | if (IS_ERR(from_name)) { |
| 2585 | rc = PTR_ERR(from_name); |
| 2586 | goto cifs_rename_exit; |
| 2587 | } |
| 2588 | |
| 2589 | to_name = build_path_from_dentry(target_dentry, page2); |
| 2590 | if (IS_ERR(to_name)) { |
| 2591 | rc = PTR_ERR(to_name); |
| 2592 | goto cifs_rename_exit; |
| 2593 | } |
| 2594 | |
| 2595 | cifs_close_deferred_file_under_dentry(tcon, source_dentry); |
| 2596 | if (d_inode(target_dentry) != NULL) { |
| 2597 | netfs_wait_for_outstanding_io(d_inode(target_dentry)); |
| 2598 | cifs_close_deferred_file_under_dentry(tcon, target_dentry); |
| 2599 | } |
| 2600 | |
| 2601 | rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry, |
| 2602 | to_name); |
| 2603 | |
| 2604 | if (rc == -EACCES) { |
| 2605 | while (retry_count < 3) { |
| 2606 | cifs_close_all_deferred_files(tcon); |
| 2607 | rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry, |
| 2608 | to_name); |
| 2609 | if (rc != -EACCES) |
| 2610 | break; |
| 2611 | retry_count++; |
| 2612 | } |
| 2613 | } |
| 2614 | |
| 2615 | if (!rc) |
| 2616 | rehash = false; |
| 2617 | /* |
| 2618 | * No-replace is the natural behavior for CIFS, so skip unlink hacks. |
| 2619 | */ |
| 2620 | if (flags & RENAME_NOREPLACE) |
| 2621 | goto cifs_rename_exit; |
| 2622 | |
| 2623 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 2624 | if (rc == -EEXIST && tcon->unix_ext) { |
| 2625 | /* |
| 2626 | * Are src and dst hardlinks of same inode? We can only tell |
| 2627 | * with unix extensions enabled. |
| 2628 | */ |
| 2629 | info_buf_source = |
| 2630 | kmalloc_objs(FILE_UNIX_BASIC_INFO, 2); |
| 2631 | if (info_buf_source == NULL) { |
| 2632 | rc = -ENOMEM; |
| 2633 | goto cifs_rename_exit; |
| 2634 | } |
| 2635 | |
| 2636 | info_buf_target = info_buf_source + 1; |
| 2637 | tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name, |
| 2638 | info_buf_source, |
| 2639 | cifs_sb->local_nls, |
| 2640 | cifs_remap(cifs_sb)); |
| 2641 | if (tmprc != 0) |
| 2642 | goto unlink_target; |
| 2643 | |
| 2644 | tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name, |
| 2645 | info_buf_target, |
| 2646 | cifs_sb->local_nls, |
| 2647 | cifs_remap(cifs_sb)); |
| 2648 | |
| 2649 | if (tmprc == 0 && (info_buf_source->UniqueId == |
| 2650 | info_buf_target->UniqueId)) { |
| 2651 | /* same file, POSIX says that this is a noop */ |
| 2652 | rc = 0; |
| 2653 | goto cifs_rename_exit; |
| 2654 | } |
| 2655 | } |
| 2656 | /* |
| 2657 | * else ... BB we could add the same check for Windows by |
| 2658 | * checking the UniqueId via FILE_INTERNAL_INFO |
| 2659 | */ |
| 2660 | |
| 2661 | unlink_target: |
| 2662 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 2663 | if (d_really_is_positive(target_dentry)) { |
| 2664 | if (!rc) { |
| 2665 | struct inode *inode = d_inode(target_dentry); |
| 2666 | |
| 2667 | /* Update the target link count after rename. */ |
| 2668 | if (S_ISDIR(inode->i_mode)) { |
| 2669 | drop_cached_dir_by_name(xid, tcon, to_name, cifs_sb); |
| 2670 | spin_lock(&inode->i_lock); |
| 2671 | i_size_write(inode, 0); |
| 2672 | clear_nlink(inode); |
| 2673 | spin_unlock(&inode->i_lock); |
| 2674 | set_bit(CIFS_INO_DELETE_PENDING, &CIFS_I(inode)->flags); |
| 2675 | CIFS_I(inode)->time = 0; /* force reval */ |
| 2676 | inode_set_ctime_current(inode); |
| 2677 | inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); |
| 2678 | } else { |
| 2679 | cifs_mark_open_handles_for_deleted_file(inode, to_name); |
| 2680 | cifs_drop_nlink(inode); |
| 2681 | inode_set_ctime_current(inode); |
| 2682 | } |
| 2683 | } else if (rc == -EACCES || rc == -EEXIST) { |
| 2684 | /* |
| 2685 | * Rename failed, possibly due to a busy target. |
| 2686 | * Retry it by unliking the target first. |
| 2687 | */ |
| 2688 | if (d_is_dir(target_dentry)) { |
| 2689 | tmprc = cifs_rmdir(target_dir, target_dentry); |
| 2690 | } else { |
| 2691 | tmprc = __cifs_unlink(target_dir, target_dentry, |
| 2692 | server->vals->protocol_id > SMB10_PROT_ID); |
| 2693 | } |
| 2694 | if (tmprc) { |
| 2695 | /* |
| 2696 | * Some servers will return STATUS_ACCESS_DENIED |
| 2697 | * or STATUS_DIRECTORY_NOT_EMPTY when failing to |
| 2698 | * rename a non-empty directory. Make sure to |
| 2699 | * propagate the appropriate error back to |
| 2700 | * userspace. |
| 2701 | */ |
| 2702 | if (tmprc == -EEXIST || tmprc == -ENOTEMPTY) |
| 2703 | rc = tmprc; |
| 2704 | goto cifs_rename_exit; |
| 2705 | } |
| 2706 | rc = cifs_do_rename(xid, source_dentry, from_name, |
| 2707 | target_dentry, to_name); |
| 2708 | if (!rc) |
| 2709 | rehash = false; |
| 2710 | } |
| 2711 | } |
| 2712 | |
| 2713 | /* force revalidate to go get info when needed */ |
| 2714 | if (!rc) { |
| 2715 | cifs_invalidate_cached_dir(tcon, source_dentry->d_parent); |
| 2716 | if (target_dentry->d_parent != source_dentry->d_parent) |
| 2717 | cifs_invalidate_cached_dir(tcon, target_dentry->d_parent); |
| 2718 | } |
| 2719 | |
| 2720 | CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0; |
| 2721 | |
| 2722 | cifs_rename_exit: |
| 2723 | if (rehash) |
| 2724 | d_rehash(target_dentry); |
| 2725 | kfree(info_buf_source); |
| 2726 | free_dentry_path(page2); |
| 2727 | free_dentry_path(page1); |
| 2728 | free_xid(xid); |
| 2729 | cifs_put_tlink(tlink); |
| 2730 | return rc; |
| 2731 | } |
| 2732 | |
| 2733 | static bool |
| 2734 | cifs_dentry_needs_reval(struct dentry *dentry) |
| 2735 | { |
| 2736 | struct inode *inode = d_inode(dentry); |
| 2737 | struct cifsInodeInfo *cifs_i = CIFS_I(inode); |
| 2738 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode); |
| 2739 | struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); |
| 2740 | struct cached_fid *cfid = NULL; |
| 2741 | |
| 2742 | if (test_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags) || |
| 2743 | test_bit(CIFS_INO_TMPFILE, &cifs_i->flags)) |
| 2744 | return false; |
| 2745 | if (cifs_i->time == 0) |
| 2746 | return true; |
| 2747 | |
| 2748 | if (CIFS_CACHE_READ(cifs_i)) |
| 2749 | return false; |
| 2750 | |
| 2751 | if (!lookupCacheEnabled) |
| 2752 | return true; |
| 2753 | |
| 2754 | if (!open_cached_dir_by_dentry(tcon, dentry->d_parent, &cfid)) { |
| 2755 | if (cifs_i->time > cfid->time) { |
| 2756 | close_cached_dir(cfid); |
| 2757 | return false; |
| 2758 | } |
| 2759 | close_cached_dir(cfid); |
| 2760 | } |
| 2761 | /* |
| 2762 | * depending on inode type, check if attribute caching disabled for |
| 2763 | * files or directories |
| 2764 | */ |
| 2765 | if (S_ISDIR(inode->i_mode)) { |
| 2766 | if (!cifs_sb->ctx->acdirmax) |
| 2767 | return true; |
| 2768 | if (!time_in_range(jiffies, cifs_i->time, |
| 2769 | cifs_i->time + cifs_sb->ctx->acdirmax)) |
| 2770 | return true; |
| 2771 | } else { /* file */ |
| 2772 | if (!cifs_sb->ctx->acregmax) |
| 2773 | return true; |
| 2774 | if (!time_in_range(jiffies, cifs_i->time, |
| 2775 | cifs_i->time + cifs_sb->ctx->acregmax)) |
| 2776 | return true; |
| 2777 | } |
| 2778 | |
| 2779 | /* hardlinked files w/ noserverino get "special" treatment */ |
| 2780 | if (!(cifs_sb_flags(cifs_sb) & CIFS_MOUNT_SERVER_INUM) && |
| 2781 | S_ISREG(inode->i_mode) && inode->i_nlink != 1) |
| 2782 | return true; |
| 2783 | |
| 2784 | return false; |
| 2785 | } |
| 2786 | |
| 2787 | /** |
| 2788 | * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks |
| 2789 | * |
| 2790 | * @key: currently unused |
| 2791 | * @mode: the task state to sleep in |
| 2792 | */ |
| 2793 | static int |
| 2794 | cifs_wait_bit_killable(struct wait_bit_key *key, int mode) |
| 2795 | { |
| 2796 | schedule(); |
| 2797 | if (signal_pending_state(mode, current)) |
| 2798 | return -ERESTARTSYS; |
| 2799 | return 0; |
| 2800 | } |
| 2801 | |
| 2802 | int |
| 2803 | cifs_revalidate_mapping(struct inode *inode) |
| 2804 | { |
| 2805 | struct cifsInodeInfo *cifs_inode = CIFS_I(inode); |
| 2806 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode); |
| 2807 | unsigned long *flags = &cifs_inode->flags; |
| 2808 | int rc; |
| 2809 | |
| 2810 | /* swapfiles are not supposed to be shared */ |
| 2811 | if (IS_SWAPFILE(inode)) |
| 2812 | return 0; |
| 2813 | |
| 2814 | rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable, |
| 2815 | TASK_KILLABLE|TASK_FREEZABLE_UNSAFE); |
| 2816 | if (rc) |
| 2817 | return rc; |
| 2818 | |
| 2819 | if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) { |
| 2820 | /* for cache=singleclient, do not invalidate */ |
| 2821 | if (cifs_sb_flags(cifs_sb) & CIFS_MOUNT_RW_CACHE) |
| 2822 | goto skip_invalidate; |
| 2823 | |
| 2824 | spin_lock(&inode->i_lock); |
| 2825 | netfs_write_zero_point(inode, netfs_inode(inode)->_remote_i_size); |
| 2826 | spin_unlock(&inode->i_lock); |
| 2827 | rc = filemap_invalidate_inode(inode, true, 0, LLONG_MAX); |
| 2828 | if (rc) { |
| 2829 | cifs_dbg(VFS, "%s: invalidate inode %p failed with rc %d\n", |
| 2830 | __func__, inode, rc); |
| 2831 | set_bit(CIFS_INO_INVALID_MAPPING, flags); |
| 2832 | } |
| 2833 | } |
| 2834 | |
| 2835 | skip_invalidate: |
| 2836 | clear_and_wake_up_bit(CIFS_INO_LOCK, flags); |
| 2837 | |
| 2838 | return rc; |
| 2839 | } |
| 2840 | |
| 2841 | int |
| 2842 | cifs_zap_mapping(struct inode *inode) |
| 2843 | { |
| 2844 | set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags); |
| 2845 | return cifs_revalidate_mapping(inode); |
| 2846 | } |
| 2847 | |
| 2848 | int cifs_revalidate_file_attr(struct file *filp) |
| 2849 | { |
| 2850 | int rc = 0; |
| 2851 | struct dentry *dentry = file_dentry(filp); |
| 2852 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 2853 | struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data; |
| 2854 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 2855 | |
| 2856 | if (!cifs_dentry_needs_reval(dentry)) |
| 2857 | return rc; |
| 2858 | |
| 2859 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 2860 | if (tlink_tcon(cfile->tlink)->unix_ext) |
| 2861 | rc = cifs_get_file_info_unix(filp); |
| 2862 | else |
| 2863 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 2864 | rc = cifs_get_file_info(filp); |
| 2865 | |
| 2866 | return rc; |
| 2867 | } |
| 2868 | |
| 2869 | int cifs_revalidate_dentry_attr(struct dentry *dentry) |
| 2870 | { |
| 2871 | unsigned int xid; |
| 2872 | int rc = 0; |
| 2873 | struct inode *inode = d_inode(dentry); |
| 2874 | struct super_block *sb = dentry->d_sb; |
| 2875 | const char *full_path; |
| 2876 | void *page; |
| 2877 | int count = 0; |
| 2878 | |
| 2879 | if (inode == NULL) |
| 2880 | return -ENOENT; |
| 2881 | |
| 2882 | if (!cifs_dentry_needs_reval(dentry)) |
| 2883 | return rc; |
| 2884 | |
| 2885 | xid = get_xid(); |
| 2886 | |
| 2887 | page = alloc_dentry_path(); |
| 2888 | full_path = build_path_from_dentry(dentry, page); |
| 2889 | if (IS_ERR(full_path)) { |
| 2890 | rc = PTR_ERR(full_path); |
| 2891 | goto out; |
| 2892 | } |
| 2893 | |
| 2894 | cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n", |
| 2895 | full_path, inode, icount_read_once(inode), |
| 2896 | dentry, cifs_get_time(dentry), jiffies); |
| 2897 | |
| 2898 | again: |
| 2899 | if (cifs_sb_master_tcon(CIFS_SB(sb))->posix_extensions) { |
| 2900 | rc = smb311_posix_get_inode_info(&inode, full_path, |
| 2901 | NULL, sb, xid); |
| 2902 | } else if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext) { |
| 2903 | rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid); |
| 2904 | } else { |
| 2905 | rc = cifs_get_inode_info(&inode, full_path, NULL, sb, |
| 2906 | xid, NULL); |
| 2907 | } |
| 2908 | if (rc == -EAGAIN && count++ < 10) |
| 2909 | goto again; |
| 2910 | out: |
| 2911 | free_dentry_path(page); |
| 2912 | free_xid(xid); |
| 2913 | |
| 2914 | return rc; |
| 2915 | } |
| 2916 | |
| 2917 | int cifs_revalidate_file(struct file *filp) |
| 2918 | { |
| 2919 | int rc; |
| 2920 | struct inode *inode = file_inode(filp); |
| 2921 | |
| 2922 | rc = cifs_revalidate_file_attr(filp); |
| 2923 | if (rc) |
| 2924 | return rc; |
| 2925 | |
| 2926 | return cifs_revalidate_mapping(inode); |
| 2927 | } |
| 2928 | |
| 2929 | /* revalidate a dentry's inode attributes */ |
| 2930 | int cifs_revalidate_dentry(struct dentry *dentry) |
| 2931 | { |
| 2932 | int rc; |
| 2933 | struct inode *inode = d_inode(dentry); |
| 2934 | |
| 2935 | rc = cifs_revalidate_dentry_attr(dentry); |
| 2936 | if (rc) |
| 2937 | return rc; |
| 2938 | |
| 2939 | return cifs_revalidate_mapping(inode); |
| 2940 | } |
| 2941 | |
| 2942 | int cifs_getattr(struct mnt_idmap *idmap, const struct path *path, |
| 2943 | struct kstat *stat, u32 request_mask, unsigned int flags) |
| 2944 | { |
| 2945 | struct cifs_sb_info *cifs_sb = CIFS_SB(path->dentry); |
| 2946 | struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); |
| 2947 | struct dentry *dentry = path->dentry; |
| 2948 | struct inode *inode = d_inode(dentry); |
| 2949 | unsigned int sbflags; |
| 2950 | int rc; |
| 2951 | |
| 2952 | if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb)))) |
| 2953 | return smb_EIO(smb_eio_trace_forced_shutdown); |
| 2954 | |
| 2955 | /* |
| 2956 | * We need to be sure that all dirty pages are written and the server |
| 2957 | * has actual ctime, mtime and file length. |
| 2958 | */ |
| 2959 | if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_SIZE | STATX_BLOCKS)) && |
| 2960 | !CIFS_CACHE_READ(CIFS_I(inode)) && |
| 2961 | inode->i_mapping && inode->i_mapping->nrpages != 0) { |
| 2962 | rc = filemap_fdatawait(inode->i_mapping); |
| 2963 | if (rc) { |
| 2964 | mapping_set_error(inode->i_mapping, rc); |
| 2965 | return rc; |
| 2966 | } |
| 2967 | } |
| 2968 | |
| 2969 | if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_FORCE_SYNC) |
| 2970 | CIFS_I(inode)->time = 0; /* force revalidate */ |
| 2971 | |
| 2972 | /* |
| 2973 | * If the caller doesn't require syncing, only sync if |
| 2974 | * necessary (e.g. due to earlier truncate or setattr |
| 2975 | * invalidating the cached metadata) |
| 2976 | */ |
| 2977 | if (((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) || |
| 2978 | (CIFS_I(inode)->time == 0)) { |
| 2979 | rc = cifs_revalidate_dentry_attr(dentry); |
| 2980 | if (rc) |
| 2981 | return rc; |
| 2982 | } |
| 2983 | |
| 2984 | generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat); |
| 2985 | stat->blksize = cifs_sb->ctx->bsize; |
| 2986 | stat->ino = CIFS_I(inode)->uniqueid; |
| 2987 | |
| 2988 | /* old CIFS Unix Extensions doesn't return create time */ |
| 2989 | if (CIFS_I(inode)->createtime) { |
| 2990 | stat->result_mask |= STATX_BTIME; |
| 2991 | stat->btime = |
| 2992 | cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime)); |
| 2993 | } |
| 2994 | |
| 2995 | stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED); |
| 2996 | if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED) |
| 2997 | stat->attributes |= STATX_ATTR_COMPRESSED; |
| 2998 | if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED) |
| 2999 | stat->attributes |= STATX_ATTR_ENCRYPTED; |
| 3000 | |
| 3001 | /* |
| 3002 | * If on a multiuser mount without unix extensions, posix extensions |
| 3003 | * or cifsacl being enabled, and the admin hasn't overridden them, |
| 3004 | * set the ownership to the fsuid/fsgid of the current process. |
| 3005 | */ |
| 3006 | sbflags = cifs_sb_flags(cifs_sb); |
| 3007 | if ((sbflags & CIFS_MOUNT_MULTIUSER) && |
| 3008 | !(sbflags & CIFS_MOUNT_CIFS_ACL) && |
| 3009 | !tcon->unix_ext && !tcon->posix_extensions) { |
| 3010 | if (!(sbflags & CIFS_MOUNT_OVERR_UID)) |
| 3011 | stat->uid = current_fsuid(); |
| 3012 | if (!(sbflags & CIFS_MOUNT_OVERR_GID)) |
| 3013 | stat->gid = current_fsgid(); |
| 3014 | } |
| 3015 | return 0; |
| 3016 | } |
| 3017 | |
| 3018 | int cifs_fiemap(struct inode *inode, struct fiemap_extent_info *fei, u64 start, |
| 3019 | u64 len) |
| 3020 | { |
| 3021 | struct cifsInodeInfo *cifs_i = CIFS_I(inode); |
| 3022 | struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_i->netfs.inode.i_sb); |
| 3023 | struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); |
| 3024 | struct TCP_Server_Info *server = tcon->ses->server; |
| 3025 | struct cifsFileInfo *cfile; |
| 3026 | int rc; |
| 3027 | |
| 3028 | if (unlikely(cifs_forced_shutdown(cifs_sb))) |
| 3029 | return smb_EIO(smb_eio_trace_forced_shutdown); |
| 3030 | |
| 3031 | /* |
| 3032 | * We need to be sure that all dirty pages are written as they |
| 3033 | * might fill holes on the server. |
| 3034 | */ |
| 3035 | if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping && |
| 3036 | inode->i_mapping->nrpages != 0) { |
| 3037 | rc = filemap_fdatawait(inode->i_mapping); |
| 3038 | if (rc) { |
| 3039 | mapping_set_error(inode->i_mapping, rc); |
| 3040 | return rc; |
| 3041 | } |
| 3042 | } |
| 3043 | |
| 3044 | cfile = find_readable_file(cifs_i, FIND_ANY); |
| 3045 | if (cfile == NULL) |
| 3046 | return -EINVAL; |
| 3047 | |
| 3048 | if (server->ops->fiemap) { |
| 3049 | rc = server->ops->fiemap(tcon, cfile, fei, start, len); |
| 3050 | cifsFileInfo_put(cfile); |
| 3051 | return rc; |
| 3052 | } |
| 3053 | |
| 3054 | cifsFileInfo_put(cfile); |
| 3055 | return -EOPNOTSUPP; |
| 3056 | } |
| 3057 | |
| 3058 | void cifs_setsize(struct inode *inode, loff_t old_size, loff_t offset) |
| 3059 | { |
| 3060 | u64 blocks = CIFS_INO_BLOCKS(offset); |
| 3061 | |
| 3062 | spin_lock(&inode->i_lock); |
| 3063 | i_size_write(inode, offset); |
| 3064 | /* |
| 3065 | * Extending EOF does not allocate the intervening range. Only clamp |
| 3066 | * i_blocks on shrink; allocation growth comes from writes or from the |
| 3067 | * server-reported AllocationSize. |
| 3068 | */ |
| 3069 | if (offset < old_size && (u64)inode->i_blocks > blocks) |
| 3070 | inode->i_blocks = blocks; |
| 3071 | spin_unlock(&inode->i_lock); |
| 3072 | inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); |
| 3073 | |
| 3074 | /* |
| 3075 | * Zero the tail of the folio straddling the old EOF so data dirtied |
| 3076 | * past EOF through an mmap isn't exposed. truncate_pagecache() then |
| 3077 | * drops any pagecache beyond the new EOF, as in truncate_setsize(). |
| 3078 | */ |
| 3079 | if (offset > old_size) |
| 3080 | netfs_clear_stale_post_isize(inode, old_size, offset); |
| 3081 | |
| 3082 | truncate_pagecache(inode, offset); |
| 3083 | netfs_wait_for_outstanding_io(inode); |
| 3084 | } |
| 3085 | |
| 3086 | void cifs_resize_file_locked(struct inode *inode, loff_t old_size, |
| 3087 | loff_t offset) |
| 3088 | { |
| 3089 | struct fscache_cookie *cookie = cifs_inode_cookie(inode); |
| 3090 | |
| 3091 | lockdep_assert_held_write(&inode->i_rwsem); |
| 3092 | |
| 3093 | netfs_resize_file(netfs_inode(inode), offset, true); |
| 3094 | cifs_setsize(inode, old_size, offset); |
| 3095 | |
| 3096 | if (!cookie) |
| 3097 | return; |
| 3098 | |
| 3099 | fscache_use_cookie(cookie, true); |
| 3100 | fscache_resize_cookie(cookie, offset); |
| 3101 | cifs_fscache_unuse_inode_cookie(inode, true); |
| 3102 | } |
| 3103 | |
| 3104 | int cifs_file_set_size(const unsigned int xid, struct dentry *dentry, |
| 3105 | const char *full_path, struct cifsFileInfo *open_file, |
| 3106 | loff_t size) |
| 3107 | { |
| 3108 | struct inode *inode = d_inode(dentry); |
| 3109 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 3110 | struct cifsInodeInfo *cifsInode = CIFS_I(inode); |
| 3111 | loff_t old_size = i_size_read(inode); |
| 3112 | struct tcon_link *tlink = NULL; |
| 3113 | struct cifs_tcon *tcon = NULL; |
| 3114 | struct TCP_Server_Info *server; |
| 3115 | int rc = -EINVAL; |
| 3116 | |
| 3117 | /* |
| 3118 | * To avoid spurious oplock breaks from server, in the case of |
| 3119 | * inodes that we already have open, avoid doing path based |
| 3120 | * setting of file size if we can do it by handle. |
| 3121 | * This keeps our caching token (oplock) and avoids timeouts |
| 3122 | * when the local oplock break takes longer to flush |
| 3123 | * writebehind data than the SMB timeout for the SetPathInfo |
| 3124 | * request would allow |
| 3125 | */ |
| 3126 | if (open_file && (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE)) { |
| 3127 | tcon = tlink_tcon(open_file->tlink); |
| 3128 | server = tcon->ses->server; |
| 3129 | rc = server->ops->set_file_size(xid, tcon, |
| 3130 | open_file, |
| 3131 | size, false); |
| 3132 | cifs_dbg(FYI, "%s: set_file_size: rc = %d\n", __func__, rc); |
| 3133 | } else { |
| 3134 | open_file = find_writable_file(cifsInode, FIND_FSUID_ONLY); |
| 3135 | if (open_file) { |
| 3136 | tcon = tlink_tcon(open_file->tlink); |
| 3137 | server = tcon->ses->server; |
| 3138 | rc = server->ops->set_file_size(xid, tcon, |
| 3139 | open_file, |
| 3140 | size, false); |
| 3141 | cifs_dbg(FYI, "%s: set_file_size: rc = %d\n", __func__, rc); |
| 3142 | cifsFileInfo_put(open_file); |
| 3143 | tcon = NULL; |
| 3144 | } |
| 3145 | } |
| 3146 | |
| 3147 | if (!rc) |
| 3148 | goto set_size_out; |
| 3149 | |
| 3150 | if (tcon == NULL) { |
| 3151 | tlink = cifs_sb_tlink(cifs_sb); |
| 3152 | if (IS_ERR(tlink)) |
| 3153 | return PTR_ERR(tlink); |
| 3154 | tcon = tlink_tcon(tlink); |
| 3155 | server = tcon->ses->server; |
| 3156 | } |
| 3157 | |
| 3158 | /* |
| 3159 | * Set file size by pathname rather than by handle either because no |
| 3160 | * valid, writeable file handle for it was found or because there was |
| 3161 | * an error setting it by handle. |
| 3162 | */ |
| 3163 | rc = server->ops->set_path_size(xid, tcon, full_path, size, |
| 3164 | cifs_sb, false, dentry); |
| 3165 | cifs_dbg(FYI, "%s: SetEOF by path (setattrs) rc = %d\n", __func__, rc); |
| 3166 | cifs_put_tlink(tlink); |
| 3167 | |
| 3168 | set_size_out: |
| 3169 | if (rc == 0) |
| 3170 | cifs_resize_file_locked(inode, old_size, size); |
| 3171 | |
| 3172 | return rc; |
| 3173 | } |
| 3174 | |
| 3175 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 3176 | static int |
| 3177 | cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs) |
| 3178 | { |
| 3179 | int rc; |
| 3180 | unsigned int xid; |
| 3181 | const char *full_path; |
| 3182 | void *page = alloc_dentry_path(); |
| 3183 | struct inode *inode = d_inode(direntry); |
| 3184 | struct cifsInodeInfo *cifsInode = CIFS_I(inode); |
| 3185 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode); |
| 3186 | struct tcon_link *tlink; |
| 3187 | struct cifs_tcon *pTcon; |
| 3188 | struct cifs_unix_set_info_args *args = NULL; |
| 3189 | struct cifsFileInfo *open_file = NULL; |
| 3190 | |
| 3191 | cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n", |
| 3192 | direntry, attrs->ia_valid); |
| 3193 | |
| 3194 | xid = get_xid(); |
| 3195 | |
| 3196 | if (cifs_sb_flags(cifs_sb) & CIFS_MOUNT_NO_PERM) |
| 3197 | attrs->ia_valid |= ATTR_FORCE; |
| 3198 | |
| 3199 | rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs); |
| 3200 | if (rc < 0) |
| 3201 | goto out; |
| 3202 | |
| 3203 | if (attrs->ia_valid & ATTR_FILE) |
| 3204 | open_file = attrs->ia_file->private_data; |
| 3205 | |
| 3206 | full_path = build_path_from_dentry(direntry, page); |
| 3207 | if (IS_ERR(full_path)) { |
| 3208 | rc = PTR_ERR(full_path); |
| 3209 | goto out; |
| 3210 | } |
| 3211 | |
| 3212 | /* |
| 3213 | * Attempt to flush data before changing attributes. We need to do |
| 3214 | * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the |
| 3215 | * ownership or mode then we may also need to do this. Here, we take |
| 3216 | * the safe way out and just do the flush on all setattr requests. If |
| 3217 | * the flush returns error, store it to report later and continue. |
| 3218 | * |
| 3219 | * BB: This should be smarter. Why bother flushing pages that |
| 3220 | * will be truncated anyway? Also, should we error out here if |
| 3221 | * the flush returns error? |
| 3222 | */ |
| 3223 | rc = filemap_write_and_wait(inode->i_mapping); |
| 3224 | if (is_interrupt_error(rc)) { |
| 3225 | rc = -ERESTARTSYS; |
| 3226 | goto out; |
| 3227 | } |
| 3228 | |
| 3229 | mapping_set_error(inode->i_mapping, rc); |
| 3230 | rc = 0; |
| 3231 | |
| 3232 | if (attrs->ia_valid & ATTR_SIZE) { |
| 3233 | if (attrs->ia_size != i_size_read(inode)) { |
| 3234 | /* Stamp before RPC. On failure the stamp remains: restoring a |
| 3235 | * stale snapshot could silently erase a concurrent |
| 3236 | * _cifsFileInfo_put() close stamp. readdir is suppressed |
| 3237 | * until the stamp expires; stat() bypasses this via the |
| 3238 | * from_readdir=false path in is_size_safe_to_change() and |
| 3239 | * always returns an authoritative QUERY_INFO result. |
| 3240 | * Pairs with smp_load_acquire() in is_size_safe_to_change(). |
| 3241 | */ |
| 3242 | smp_store_release(&cifsInode->time_last_write, jiffies); |
| 3243 | } |
| 3244 | rc = cifs_file_set_size(xid, direntry, full_path, |
| 3245 | open_file, attrs->ia_size); |
| 3246 | if (rc != 0) |
| 3247 | goto out; |
| 3248 | /* |
| 3249 | * Avoid setting timestamps on the server for ftruncate(2) to |
| 3250 | * prevent it from disabling automatic timestamp updates as per |
| 3251 | * MS-FSA 2.1.4.17. |
| 3252 | */ |
| 3253 | attrs->ia_valid &= ~(ATTR_CTIME | ATTR_MTIME); |
| 3254 | } |
| 3255 | |
| 3256 | /* |
| 3257 | * This function is only called when Unix extensions are in effect, |
| 3258 | * so the mode is always sent to and stored on the server. Do not |
| 3259 | * skip the mode change when clearing setuid/setgid bits: dropping |
| 3260 | * ATTR_MODE here would leave those bits set on the server after a |
| 3261 | * write, which is a security issue. |
| 3262 | */ |
| 3263 | |
| 3264 | args = kmalloc_obj(*args); |
| 3265 | if (args == NULL) { |
| 3266 | rc = -ENOMEM; |
| 3267 | goto out; |
| 3268 | } |
| 3269 | |
| 3270 | /* set up the struct */ |
| 3271 | if (attrs->ia_valid & ATTR_MODE) |
| 3272 | args->mode = attrs->ia_mode; |
| 3273 | else |
| 3274 | args->mode = NO_CHANGE_64; |
| 3275 | |
| 3276 | if (attrs->ia_valid & ATTR_UID) |
| 3277 | args->uid = attrs->ia_uid; |
| 3278 | else |
| 3279 | args->uid = INVALID_UID; /* no change */ |
| 3280 | |
| 3281 | if (attrs->ia_valid & ATTR_GID) |
| 3282 | args->gid = attrs->ia_gid; |
| 3283 | else |
| 3284 | args->gid = INVALID_GID; /* no change */ |
| 3285 | |
| 3286 | if (attrs->ia_valid & ATTR_ATIME) |
| 3287 | args->atime = cifs_UnixTimeToNT(attrs->ia_atime); |
| 3288 | else |
| 3289 | args->atime = NO_CHANGE_64; |
| 3290 | |
| 3291 | if (attrs->ia_valid & ATTR_MTIME) |
| 3292 | args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime); |
| 3293 | else |
| 3294 | args->mtime = NO_CHANGE_64; |
| 3295 | |
| 3296 | if (attrs->ia_valid & ATTR_CTIME) |
| 3297 | args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime); |
| 3298 | else |
| 3299 | args->ctime = NO_CHANGE_64; |
| 3300 | |
| 3301 | args->device = 0; |
| 3302 | rc = -EINVAL; |
| 3303 | if (open_file && (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE)) { |
| 3304 | pTcon = tlink_tcon(open_file->tlink); |
| 3305 | rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, |
| 3306 | open_file->fid.netfid, |
| 3307 | open_file->pid); |
| 3308 | } else { |
| 3309 | open_file = find_writable_file(cifsInode, FIND_FSUID_ONLY); |
| 3310 | if (open_file) { |
| 3311 | pTcon = tlink_tcon(open_file->tlink); |
| 3312 | rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, |
| 3313 | open_file->fid.netfid, |
| 3314 | open_file->pid); |
| 3315 | cifsFileInfo_put(open_file); |
| 3316 | } |
| 3317 | } |
| 3318 | |
| 3319 | if (rc) { |
| 3320 | tlink = cifs_sb_tlink(cifs_sb); |
| 3321 | if (IS_ERR(tlink)) { |
| 3322 | rc = PTR_ERR(tlink); |
| 3323 | goto out; |
| 3324 | } |
| 3325 | pTcon = tlink_tcon(tlink); |
| 3326 | rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args, |
| 3327 | cifs_sb->local_nls, |
| 3328 | cifs_remap(cifs_sb)); |
| 3329 | cifs_put_tlink(tlink); |
| 3330 | } |
| 3331 | |
| 3332 | if (rc) |
| 3333 | goto out; |
| 3334 | |
| 3335 | setattr_copy(&nop_mnt_idmap, inode, attrs); |
| 3336 | mark_inode_dirty(inode); |
| 3337 | |
| 3338 | /* force revalidate when any of these times are set since some |
| 3339 | of the fs types (eg ext3, fat) do not have fine enough |
| 3340 | time granularity to match protocol, and we do not have a |
| 3341 | a way (yet) to query the server fs's time granularity (and |
| 3342 | whether it rounds times down). |
| 3343 | */ |
| 3344 | if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME)) |
| 3345 | cifsInode->time = 0; |
| 3346 | out: |
| 3347 | kfree(args); |
| 3348 | free_dentry_path(page); |
| 3349 | free_xid(xid); |
| 3350 | return rc; |
| 3351 | } |
| 3352 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 3353 | |
| 3354 | static int |
| 3355 | cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs) |
| 3356 | { |
| 3357 | struct inode *inode = d_inode(direntry); |
| 3358 | struct cifsInodeInfo *cifsInode = CIFS_I(inode); |
| 3359 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode); |
| 3360 | unsigned int sbflags = cifs_sb_flags(cifs_sb); |
| 3361 | struct cifsFileInfo *cfile = NULL; |
| 3362 | void *page = alloc_dentry_path(); |
| 3363 | __u64 mode = NO_CHANGE_64; |
| 3364 | kuid_t uid = INVALID_UID; |
| 3365 | kgid_t gid = INVALID_GID; |
| 3366 | const char *full_path; |
| 3367 | __u32 dosattr = 0; |
| 3368 | int rc = -EACCES; |
| 3369 | unsigned int xid; |
| 3370 | |
| 3371 | xid = get_xid(); |
| 3372 | |
| 3373 | cifs_dbg(FYI, "setattr on file %pd attrs->ia_valid 0x%x\n", |
| 3374 | direntry, attrs->ia_valid); |
| 3375 | |
| 3376 | if (sbflags & CIFS_MOUNT_NO_PERM) |
| 3377 | attrs->ia_valid |= ATTR_FORCE; |
| 3378 | |
| 3379 | rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs); |
| 3380 | if (rc < 0) |
| 3381 | goto cifs_setattr_exit; |
| 3382 | |
| 3383 | if (attrs->ia_valid & ATTR_FILE) |
| 3384 | cfile = attrs->ia_file->private_data; |
| 3385 | |
| 3386 | full_path = build_path_from_dentry(direntry, page); |
| 3387 | if (IS_ERR(full_path)) { |
| 3388 | rc = PTR_ERR(full_path); |
| 3389 | goto cifs_setattr_exit; |
| 3390 | } |
| 3391 | |
| 3392 | /* |
| 3393 | * Attempt to flush data before changing attributes. We need to do |
| 3394 | * this for ATTR_SIZE and ATTR_MTIME. If the flush of the data |
| 3395 | * returns error, store it to report later and continue. |
| 3396 | * |
| 3397 | * BB: This should be smarter. Why bother flushing pages that |
| 3398 | * will be truncated anyway? Also, should we error out here if |
| 3399 | * the flush returns error? Do we need to check for ATTR_MTIME_SET flag? |
| 3400 | */ |
| 3401 | if (attrs->ia_valid & (ATTR_MTIME | ATTR_SIZE | ATTR_CTIME)) { |
| 3402 | rc = filemap_write_and_wait(inode->i_mapping); |
| 3403 | if (is_interrupt_error(rc)) { |
| 3404 | rc = -ERESTARTSYS; |
| 3405 | goto cifs_setattr_exit; |
| 3406 | } |
| 3407 | mapping_set_error(inode->i_mapping, rc); |
| 3408 | } |
| 3409 | |
| 3410 | rc = 0; |
| 3411 | |
| 3412 | if (attrs->ia_valid & ATTR_MTIME) { |
| 3413 | rc = cifs_file_flush(xid, inode, cfile); |
| 3414 | if (rc) |
| 3415 | goto cifs_setattr_exit; |
| 3416 | } |
| 3417 | |
| 3418 | if (attrs->ia_valid & ATTR_SIZE) { |
| 3419 | if (attrs->ia_size != i_size_read(inode)) { |
| 3420 | /* Stamp before RPC. On failure the stamp remains: restoring a |
| 3421 | * stale snapshot could silently erase a concurrent |
| 3422 | * _cifsFileInfo_put() close stamp. readdir is suppressed |
| 3423 | * until the stamp expires; stat() bypasses this via the |
| 3424 | * from_readdir=false path in is_size_safe_to_change() and |
| 3425 | * always returns an authoritative QUERY_INFO result. |
| 3426 | * Pairs with smp_load_acquire() in is_size_safe_to_change(). |
| 3427 | */ |
| 3428 | smp_store_release(&cifsInode->time_last_write, jiffies); |
| 3429 | } |
| 3430 | rc = cifs_file_set_size(xid, direntry, full_path, |
| 3431 | cfile, attrs->ia_size); |
| 3432 | if (rc != 0) |
| 3433 | goto cifs_setattr_exit; |
| 3434 | /* |
| 3435 | * Avoid setting timestamps on the server for ftruncate(2) to |
| 3436 | * prevent it from disabling automatic timestamp updates as per |
| 3437 | * MS-FSA 2.1.4.17. |
| 3438 | */ |
| 3439 | attrs->ia_valid &= ~(ATTR_CTIME | ATTR_MTIME); |
| 3440 | } |
| 3441 | |
| 3442 | if (attrs->ia_valid & ATTR_UID) |
| 3443 | uid = attrs->ia_uid; |
| 3444 | |
| 3445 | if (attrs->ia_valid & ATTR_GID) |
| 3446 | gid = attrs->ia_gid; |
| 3447 | |
| 3448 | if ((sbflags & (CIFS_MOUNT_CIFS_ACL | CIFS_MOUNT_MODE_FROM_SID)) || |
| 3449 | cifs_sb_master_tcon(cifs_sb)->posix_extensions) { |
| 3450 | if (uid_valid(uid) || gid_valid(gid)) { |
| 3451 | mode = NO_CHANGE_64; |
| 3452 | rc = id_mode_to_cifs_acl(inode, full_path, &mode, |
| 3453 | uid, gid); |
| 3454 | if (rc) { |
| 3455 | cifs_dbg(FYI, "%s: Setting id failed with error: %d\n", |
| 3456 | __func__, rc); |
| 3457 | goto cifs_setattr_exit; |
| 3458 | } |
| 3459 | } |
| 3460 | } else if (!(sbflags & CIFS_MOUNT_SET_UID)) { |
| 3461 | attrs->ia_valid &= ~(ATTR_UID | ATTR_GID); |
| 3462 | } |
| 3463 | |
| 3464 | /* |
| 3465 | * Skip the mode change if it is only being done to clear the |
| 3466 | * setuid/setgid bits *and* the mode is emulated via the DOS |
| 3467 | * read-only attribute (the default, non-ACL case), which cannot |
| 3468 | * represent the setuid/setgid bits anyway. |
| 3469 | * |
| 3470 | * When the mode is instead stored on the server - i.e. with the |
| 3471 | * cifsacl or modefromsid mount options (via an ACL) or with the |
| 3472 | * SMB3.1.1 POSIX extensions - the cleared mode must be pushed to |
| 3473 | * the server. Dropping ATTR_MODE here would leave the setuid/ |
| 3474 | * setgid bit set on the server after a write, which is a security |
| 3475 | * issue (the bits are not stripped as they are on local |
| 3476 | * filesystems). |
| 3477 | */ |
| 3478 | if ((attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) && |
| 3479 | !((sbflags & (CIFS_MOUNT_CIFS_ACL | CIFS_MOUNT_MODE_FROM_SID)) || |
| 3480 | cifs_sb_master_tcon(cifs_sb)->posix_extensions)) |
| 3481 | attrs->ia_valid &= ~ATTR_MODE; |
| 3482 | |
| 3483 | if (attrs->ia_valid & ATTR_MODE) { |
| 3484 | mode = attrs->ia_mode; |
| 3485 | rc = 0; |
| 3486 | if ((sbflags & (CIFS_MOUNT_CIFS_ACL | CIFS_MOUNT_MODE_FROM_SID)) || |
| 3487 | cifs_sb_master_tcon(cifs_sb)->posix_extensions) { |
| 3488 | rc = id_mode_to_cifs_acl(inode, full_path, &mode, |
| 3489 | INVALID_UID, INVALID_GID); |
| 3490 | if (rc) { |
| 3491 | cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n", |
| 3492 | __func__, rc); |
| 3493 | goto cifs_setattr_exit; |
| 3494 | } |
| 3495 | |
| 3496 | /* |
| 3497 | * In case of CIFS_MOUNT_CIFS_ACL, we cannot support all modes. |
| 3498 | * Pick up the actual mode bits that were set. |
| 3499 | */ |
| 3500 | if (mode != attrs->ia_mode) |
| 3501 | attrs->ia_mode = mode; |
| 3502 | } else |
| 3503 | if (((mode & S_IWUGO) == 0) && |
| 3504 | (cifsInode->cifsAttrs & ATTR_READONLY) == 0) { |
| 3505 | |
| 3506 | dosattr = cifsInode->cifsAttrs | ATTR_READONLY; |
| 3507 | |
| 3508 | /* fix up mode if we're not using dynperm */ |
| 3509 | if ((sbflags & CIFS_MOUNT_DYNPERM) == 0) |
| 3510 | attrs->ia_mode = inode->i_mode & ~S_IWUGO; |
| 3511 | } else if ((mode & S_IWUGO) && |
| 3512 | (cifsInode->cifsAttrs & ATTR_READONLY)) { |
| 3513 | |
| 3514 | dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY; |
| 3515 | /* Attributes of 0 are ignored */ |
| 3516 | if (dosattr == 0) |
| 3517 | dosattr |= ATTR_NORMAL; |
| 3518 | |
| 3519 | /* reset local inode permissions to normal */ |
| 3520 | if (!(sbflags & CIFS_MOUNT_DYNPERM)) { |
| 3521 | attrs->ia_mode &= ~(S_IALLUGO); |
| 3522 | if (S_ISDIR(inode->i_mode)) |
| 3523 | attrs->ia_mode |= |
| 3524 | cifs_sb->ctx->dir_mode; |
| 3525 | else |
| 3526 | attrs->ia_mode |= |
| 3527 | cifs_sb->ctx->file_mode; |
| 3528 | } |
| 3529 | } else if (!(sbflags & CIFS_MOUNT_DYNPERM)) { |
| 3530 | /* ignore mode change - ATTR_READONLY hasn't changed */ |
| 3531 | attrs->ia_valid &= ~ATTR_MODE; |
| 3532 | } |
| 3533 | } |
| 3534 | |
| 3535 | if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) || |
| 3536 | ((attrs->ia_valid & ATTR_MODE) && dosattr)) { |
| 3537 | rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr); |
| 3538 | /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */ |
| 3539 | |
| 3540 | /* Even if error on time set, no sense failing the call if |
| 3541 | the server would set the time to a reasonable value anyway, |
| 3542 | and this check ensures that we are not being called from |
| 3543 | sys_utimes in which case we ought to fail the call back to |
| 3544 | the user when the server rejects the call */ |
| 3545 | if ((rc) && (attrs->ia_valid & |
| 3546 | (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE))) |
| 3547 | rc = 0; |
| 3548 | } |
| 3549 | |
| 3550 | /* do not need local check to inode_check_ok since the server does |
| 3551 | that */ |
| 3552 | if (rc) |
| 3553 | goto cifs_setattr_exit; |
| 3554 | |
| 3555 | setattr_copy(&nop_mnt_idmap, inode, attrs); |
| 3556 | mark_inode_dirty(inode); |
| 3557 | |
| 3558 | cifs_setattr_exit: |
| 3559 | free_xid(xid); |
| 3560 | free_dentry_path(page); |
| 3561 | return rc; |
| 3562 | } |
| 3563 | |
| 3564 | int |
| 3565 | cifs_setattr(struct mnt_idmap *idmap, struct dentry *direntry, |
| 3566 | struct iattr *attrs) |
| 3567 | { |
| 3568 | struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb); |
| 3569 | int rc, retries = 0; |
| 3570 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 3571 | struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb); |
| 3572 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 3573 | |
| 3574 | if (unlikely(cifs_forced_shutdown(cifs_sb))) |
| 3575 | return smb_EIO(smb_eio_trace_forced_shutdown); |
| 3576 | /* |
| 3577 | * Avoid setting [cm]time with O_TRUNC to prevent the server from |
| 3578 | * disabling automatic timestamp updates as specified in |
| 3579 | * MS-FSA 2.1.4.17. |
| 3580 | */ |
| 3581 | if (attrs->ia_valid & ATTR_OPEN) |
| 3582 | return 0; |
| 3583 | |
| 3584 | do { |
| 3585 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 3586 | if (pTcon->unix_ext) |
| 3587 | rc = cifs_setattr_unix(direntry, attrs); |
| 3588 | else |
| 3589 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 3590 | rc = cifs_setattr_nounix(direntry, attrs); |
| 3591 | retries++; |
| 3592 | } while (is_retryable_error(rc) && retries < 2); |
| 3593 | |
| 3594 | /* BB: add cifs_setattr_legacy for really old servers */ |
| 3595 | return rc; |
| 3596 | } |