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