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