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