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