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