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