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