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