1 // SPDX-License-Identifier: LGPL-2.1 2 /* 3 * 4 * vfs operations that deal with dentries 5 * 6 * Copyright (C) International Business Machines Corp., 2002,2009 7 * Author(s): Steve French (sfrench@us.ibm.com) 8 * 9 */ 10 #include <linux/fs.h> 11 #include <linux/stat.h> 12 #include <linux/slab.h> 13 #include <linux/namei.h> 14 #include <linux/mount.h> 15 #include <linux/file.h> 16 #include "cifsfs.h" 17 #include "cifsglob.h" 18 #include "cifsproto.h" 19 #include "cifs_debug.h" 20 #include "cifs_fs_sb.h" 21 #include "cifs_unicode.h" 22 #include "fs_context.h" 23 #include "cifs_ioctl.h" 24 #include "fscache.h" 25 #include "cached_dir.h" 26 27 static void 28 renew_parental_timestamps(struct dentry *direntry) 29 { 30 /* BB check if there is a way to get the kernel to do this or if we 31 really need this */ 32 do { 33 cifs_set_time(direntry, jiffies); 34 direntry = direntry->d_parent; 35 } while (!IS_ROOT(direntry)); 36 } 37 38 char * 39 cifs_build_path_to_root(struct smb3_fs_context *ctx, struct cifs_sb_info *cifs_sb, 40 struct cifs_tcon *tcon, int add_treename) 41 { 42 int pplen = ctx->prepath ? strlen(ctx->prepath) + 1 : 0; 43 int dfsplen; 44 char *full_path = NULL; 45 46 /* if no prefix path, simply set path to the root of share to "" */ 47 if (pplen == 0) { 48 full_path = kzalloc(1, GFP_KERNEL); 49 return full_path; 50 } 51 52 if (add_treename) 53 dfsplen = strnlen(tcon->tree_name, MAX_TREE_SIZE + 1); 54 else 55 dfsplen = 0; 56 57 full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL); 58 if (full_path == NULL) 59 return full_path; 60 61 if (dfsplen) 62 memcpy(full_path, tcon->tree_name, dfsplen); 63 full_path[dfsplen] = CIFS_DIR_SEP(cifs_sb); 64 memcpy(full_path + dfsplen + 1, ctx->prepath, pplen); 65 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb)); 66 return full_path; 67 } 68 69 /* Note: caller must free return buffer */ 70 const char * 71 build_path_from_dentry(struct dentry *direntry, void *page) 72 { 73 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb); 74 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); 75 bool prefix = tcon->Flags & SMB_SHARE_IS_IN_DFS; 76 77 return build_path_from_dentry_optional_prefix(direntry, page, 78 prefix); 79 } 80 81 char *__build_path_from_dentry_optional_prefix(struct dentry *direntry, void *page, 82 const char *tree, int tree_len, 83 bool prefix) 84 { 85 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry); 86 unsigned int sbflags = cifs_sb_flags(cifs_sb); 87 char dirsep = CIFS_DIR_SEP(cifs_sb); 88 int pplen = 0; 89 int dfsplen; 90 char *s; 91 92 if (unlikely(!page)) 93 return ERR_PTR(-ENOMEM); 94 95 if (prefix) 96 dfsplen = strnlen(tree, tree_len + 1); 97 else 98 dfsplen = 0; 99 100 if (sbflags & CIFS_MOUNT_USE_PREFIX_PATH) 101 pplen = cifs_sb->prepath ? strlen(cifs_sb->prepath) + 1 : 0; 102 103 s = dentry_path_raw(direntry, page, PATH_MAX); 104 if (IS_ERR(s)) 105 return s; 106 if (!s[1]) // for root we want "", not "/" 107 s++; 108 if (s < (char *)page + pplen + dfsplen) 109 return ERR_PTR(-ENAMETOOLONG); 110 if (pplen) { 111 cifs_dbg(FYI, "using cifs_sb prepath <%s>\n", cifs_sb->prepath); 112 s -= pplen; 113 memcpy(s + 1, cifs_sb->prepath, pplen - 1); 114 *s = '/'; 115 } 116 if (dirsep != '/') { 117 /* BB test paths to Windows with '/' in the midst of prepath */ 118 strreplace(s, '/', dirsep); 119 } 120 if (dfsplen) { 121 s -= dfsplen; 122 memcpy(s, tree, dfsplen); 123 if (sbflags & CIFS_MOUNT_POSIX_PATHS) { 124 int i; 125 for (i = 0; i < dfsplen; i++) { 126 if (s[i] == '\\') 127 s[i] = '/'; 128 } 129 } 130 } 131 return s; 132 } 133 134 char *build_path_from_dentry_optional_prefix(struct dentry *direntry, void *page, 135 bool prefix) 136 { 137 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb); 138 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); 139 140 return __build_path_from_dentry_optional_prefix(direntry, page, tcon->tree_name, 141 MAX_TREE_SIZE, prefix); 142 } 143 144 /* 145 * Don't allow path components longer than the server max. 146 * Don't allow the separator character in a path component. 147 * The VFS will not allow "/", but "\" is allowed by posix. 148 */ 149 static int 150 check_name(struct dentry *direntry, struct cifs_tcon *tcon) 151 { 152 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry); 153 int i; 154 155 if (unlikely(tcon->fsAttrInfo.MaxPathNameComponentLength && 156 direntry->d_name.len > 157 le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength))) 158 return -ENAMETOOLONG; 159 160 if (!(cifs_sb_flags(cifs_sb) & CIFS_MOUNT_POSIX_PATHS)) { 161 for (i = 0; i < direntry->d_name.len; i++) { 162 if (direntry->d_name.name[i] == '\\') { 163 cifs_dbg(FYI, "Invalid file name\n"); 164 return -EINVAL; 165 } 166 } 167 } 168 return 0; 169 } 170 171 static char *alloc_parent_path(struct dentry *dentry, size_t namelen) 172 { 173 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry); 174 void *page = alloc_dentry_path(); 175 const char *path; 176 size_t size; 177 char *npath; 178 179 path = build_path_from_dentry(dentry->d_parent, page); 180 if (IS_ERR(path)) { 181 npath = ERR_CAST(path); 182 goto out; 183 } 184 185 size = strlen(path) + namelen + 2; 186 npath = kmalloc(size, GFP_KERNEL); 187 if (!npath) 188 npath = ERR_PTR(-ENOMEM); 189 else 190 scnprintf(npath, size, "%s%c", path, CIFS_DIR_SEP(cifs_sb)); 191 out: 192 free_dentry_path(page); 193 return npath; 194 } 195 196 /* Inode operations in similar order to how they appear in Linux file fs.h */ 197 static int __cifs_do_create(struct inode *dir, struct dentry *direntry, 198 const char *full_path, unsigned int xid, 199 struct tcon_link *tlink, unsigned int oflags, 200 umode_t mode, __u32 *oplock, struct cifs_fid *fid, 201 struct cifs_open_info_data *buf, 202 struct inode **inode) 203 { 204 int rc = -ENOENT; 205 int create_options = CREATE_NOT_DIR; 206 int desired_access; 207 struct cifs_sb_info *cifs_sb = CIFS_SB(dir); 208 struct cifs_tcon *tcon = tlink_tcon(tlink); 209 struct inode *newinode = NULL; 210 unsigned int sbflags = cifs_sb_flags(cifs_sb); 211 int disposition; 212 struct TCP_Server_Info *server = tcon->ses->server; 213 struct cifs_open_parms oparms; 214 struct cached_fid *parent_cfid = NULL; 215 int rdwr_for_fscache = 0; 216 __le32 lease_flags = 0; 217 218 *inode = NULL; 219 *oplock = 0; 220 if (tcon->ses->server->oplocks) 221 *oplock = REQ_OPLOCK; 222 223 /* If we're caching, we need to be able to fill in around partial writes. */ 224 if (cifs_fscache_enabled(dir) && (oflags & O_ACCMODE) == O_WRONLY) 225 rdwr_for_fscache = 1; 226 227 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 228 if (tcon->unix_ext && cap_unix(tcon->ses) && !tcon->broken_posix_open && 229 (CIFS_UNIX_POSIX_PATH_OPS_CAP & 230 le64_to_cpu(tcon->fsUnixInfo.Capability))) { 231 rc = cifs_posix_open(full_path, &newinode, dir->i_sb, mode, 232 oflags, oplock, &fid->netfid, xid); 233 switch (rc) { 234 case 0: 235 if (newinode == NULL) { 236 /* query inode info */ 237 goto cifs_create_get_file_info; 238 } 239 240 if ((oflags & __O_REGULAR) && !S_ISREG(newinode->i_mode)) { 241 CIFSSMBClose(xid, tcon, fid->netfid); 242 iput(newinode); 243 return -EFTYPE; 244 } 245 246 if (S_ISDIR(newinode->i_mode)) { 247 CIFSSMBClose(xid, tcon, fid->netfid); 248 iput(newinode); 249 return -EISDIR; 250 } 251 252 if (!S_ISREG(newinode->i_mode)) { 253 /* 254 * The server may allow us to open things like 255 * FIFOs, but the client isn't set up to deal 256 * with that. If it's not a regular file, just 257 * close it and proceed as if it were a normal 258 * lookup. 259 */ 260 CIFSSMBClose(xid, tcon, fid->netfid); 261 goto cifs_create_get_file_info; 262 } 263 /* success, no need to query */ 264 goto cifs_create_set_dentry; 265 266 case -ENOENT: 267 goto cifs_create_get_file_info; 268 269 case -EIO: 270 case -EINVAL: 271 /* 272 * EIO could indicate that (posix open) operation is not 273 * supported, despite what server claimed in capability 274 * negotiation. 275 * 276 * POSIX open in samba versions 3.3.1 and earlier could 277 * incorrectly fail with invalid parameter. 278 */ 279 tcon->broken_posix_open = true; 280 break; 281 282 case -EREMOTE: 283 case -EOPNOTSUPP: 284 /* 285 * EREMOTE indicates DFS junction, which is not handled 286 * in posix open. If either that or op not supported 287 * returned, follow the normal lookup. 288 */ 289 break; 290 291 default: 292 return rc; 293 } 294 /* 295 * fallthrough to retry, using older open call, this is case 296 * where server does not support this SMB level, and falsely 297 * claims capability (also get here for DFS case which should be 298 * rare for path not covered on files) 299 */ 300 } 301 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 302 303 desired_access = 0; 304 if (OPEN_FMODE(oflags) & FMODE_READ) 305 desired_access |= GENERIC_READ; /* is this too little? */ 306 if (OPEN_FMODE(oflags) & FMODE_WRITE) 307 desired_access |= GENERIC_WRITE; 308 if (rdwr_for_fscache == 1) 309 desired_access |= GENERIC_READ; 310 if (oflags & O_TMPFILE) 311 desired_access |= DELETE; 312 313 disposition = FILE_OVERWRITE_IF; 314 if (oflags & O_CREAT) { 315 if (oflags & O_EXCL) 316 disposition = FILE_CREATE; 317 else if (oflags & O_TRUNC) 318 disposition = FILE_OVERWRITE_IF; 319 else 320 disposition = FILE_OPEN_IF; 321 } else if (oflags & O_TMPFILE) { 322 disposition = FILE_CREATE; 323 } else { 324 cifs_dbg(FYI, "Create flag not set in create function\n"); 325 } 326 327 /* 328 * BB add processing to set equivalent of mode - e.g. via CreateX with 329 * ACLs 330 */ 331 332 if (!server->ops->open) 333 return -EOPNOTSUPP; 334 335 create_options |= cifs_open_create_options(oflags, create_options); 336 /* 337 * if we're not using unix extensions, see if we need to set 338 * ATTR_READONLY on the create call 339 */ 340 if (!tcon->unix_ext && (mode & S_IWUGO) == 0) 341 create_options |= CREATE_OPTION_READONLY; 342 343 344 retry_open: 345 if (tcon->cfids && direntry->d_parent && server->dialect >= SMB30_PROT_ID) { 346 parent_cfid = NULL; 347 spin_lock(&tcon->cfids->cfid_list_lock); 348 list_for_each_entry(parent_cfid, &tcon->cfids->entries, entry) { 349 if (parent_cfid->dentry == direntry->d_parent) { 350 cifs_dbg(FYI, "found a parent cached file handle\n"); 351 if (is_valid_cached_dir(parent_cfid)) { 352 lease_flags 353 |= SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE; 354 memcpy(fid->parent_lease_key, 355 parent_cfid->fid.lease_key, 356 SMB2_LEASE_KEY_SIZE); 357 parent_cfid->dirents.is_valid = false; 358 parent_cfid->dirents.is_failed = true; 359 } 360 break; 361 } 362 } 363 spin_unlock(&tcon->cfids->cfid_list_lock); 364 } 365 366 oparms = (struct cifs_open_parms) { 367 .tcon = tcon, 368 .cifs_sb = cifs_sb, 369 .desired_access = desired_access, 370 .create_options = cifs_create_options(cifs_sb, create_options), 371 .disposition = disposition, 372 .path = full_path, 373 .fid = fid, 374 .lease_flags = lease_flags, 375 .mode = mode, 376 }; 377 rc = server->ops->open(xid, &oparms, oplock, buf); 378 if (rc) { 379 cifs_dbg(FYI, "cifs_create returned 0x%x\n", rc); 380 if (rc == -EACCES && rdwr_for_fscache == 1) { 381 desired_access &= ~GENERIC_READ; 382 rdwr_for_fscache = 2; 383 goto retry_open; 384 } 385 return rc; 386 } 387 if (rdwr_for_fscache == 2) 388 cifs_invalidate_cache(dir, FSCACHE_INVAL_DIO_WRITE); 389 390 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 391 /* 392 * If Open reported that we actually created a file then we now have to 393 * set the mode if possible. 394 */ 395 if ((tcon->unix_ext) && (*oplock & CIFS_CREATE_ACTION)) { 396 struct cifs_unix_set_info_args args = { 397 .mode = mode, 398 .ctime = NO_CHANGE_64, 399 .atime = NO_CHANGE_64, 400 .mtime = NO_CHANGE_64, 401 .device = 0, 402 }; 403 404 if (sbflags & CIFS_MOUNT_SET_UID) { 405 args.uid = current_fsuid(); 406 if (dir->i_mode & S_ISGID) 407 args.gid = dir->i_gid; 408 else 409 args.gid = current_fsgid(); 410 } else { 411 args.uid = INVALID_UID; /* no change */ 412 args.gid = INVALID_GID; /* no change */ 413 } 414 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid->netfid, 415 current->tgid); 416 } else { 417 /* 418 * BB implement mode setting via Windows security 419 * descriptors e.g. 420 */ 421 /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/ 422 423 /* Could set r/o dos attribute if mode & 0222 == 0 */ 424 } 425 426 cifs_create_get_file_info: 427 /* server might mask mode so we have to query for it */ 428 if (tcon->unix_ext) 429 rc = cifs_get_inode_info_unix(&newinode, full_path, dir->i_sb, 430 xid); 431 else { 432 #else 433 { 434 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 435 /* TODO: Add support for calling POSIX query info here, but passing in fid */ 436 rc = cifs_get_inode_info(&newinode, full_path, buf, dir->i_sb, xid, fid); 437 if (newinode) { 438 if (server->ops->set_lease_key) 439 server->ops->set_lease_key(newinode, fid); 440 if ((*oplock & CIFS_CREATE_ACTION) && S_ISREG(newinode->i_mode)) { 441 if (sbflags & CIFS_MOUNT_DYNPERM) 442 newinode->i_mode = mode; 443 if (sbflags & CIFS_MOUNT_SET_UID) { 444 newinode->i_uid = current_fsuid(); 445 if (dir->i_mode & S_ISGID) 446 newinode->i_gid = dir->i_gid; 447 else 448 newinode->i_gid = current_fsgid(); 449 } 450 } 451 } 452 } 453 454 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 455 cifs_create_set_dentry: 456 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 457 if (rc != 0) { 458 cifs_dbg(FYI, "Create worked, get_inode_info failed rc = %d\n", 459 rc); 460 goto out_err; 461 } 462 463 if (newinode) { 464 if ((oflags & __O_REGULAR) && !S_ISREG(newinode->i_mode)) { 465 rc = -EFTYPE; 466 goto out_err; 467 } 468 if (S_ISDIR(newinode->i_mode)) { 469 rc = -EISDIR; 470 goto out_err; 471 } 472 } 473 474 *inode = newinode; 475 return rc; 476 477 out_err: 478 if (server->ops->close) 479 server->ops->close(xid, tcon, fid); 480 if (newinode) 481 iput(newinode); 482 return rc; 483 } 484 485 static int cifs_do_create(struct inode *dir, struct dentry *direntry, 486 unsigned int xid, struct tcon_link *tlink, 487 unsigned int oflags, umode_t mode, 488 __u32 *oplock, struct cifs_fid *fid, 489 struct cifs_open_info_data *buf, 490 struct inode **inode) 491 { 492 void *page = alloc_dentry_path(); 493 const char *full_path; 494 int rc; 495 496 full_path = build_path_from_dentry(direntry, page); 497 if (IS_ERR(full_path)) { 498 rc = PTR_ERR(full_path); 499 } else { 500 rc = __cifs_do_create(dir, direntry, full_path, xid, 501 tlink, oflags, mode, oplock, 502 fid, buf, inode); 503 } 504 free_dentry_path(page); 505 return rc; 506 } 507 508 509 /* 510 * Look up, create and open a CIFS file. 511 * 512 * The initial dentry state is in-lookup or hashed-negative. On success, dentry 513 * will become hashed-positive by calling d_splice_alias() if in-lookup, 514 * otherwise d_instantiate(). 515 */ 516 int cifs_atomic_open(struct inode *dir, struct dentry *direntry, 517 struct file *file, unsigned int oflags, umode_t mode) 518 { 519 struct cifs_sb_info *cifs_sb = CIFS_SB(dir); 520 struct cifs_open_info_data buf = {}; 521 struct TCP_Server_Info *server; 522 struct cifsFileInfo *file_info; 523 struct cifs_pending_open open; 524 struct cifs_fid fid = {}; 525 struct tcon_link *tlink; 526 struct cifs_tcon *tcon; 527 unsigned int sbflags; 528 struct dentry *alias; 529 struct inode *inode; 530 unsigned int xid; 531 __u32 oplock; 532 int rc; 533 534 if (unlikely(cifs_forced_shutdown(cifs_sb))) 535 return smb_EIO(smb_eio_trace_forced_shutdown); 536 537 /* 538 * Posix open is only called (at lookup time) for file create now. For 539 * opens (rather than creates), because we do not know if it is a file 540 * or directory yet, and current Samba no longer allows us to do posix 541 * open on dirs, we could end up wasting an open call on what turns out 542 * to be a dir. For file opens, we wait to call posix open till 543 * cifs_open. It could be added to atomic_open in the future but the 544 * performance tradeoff of the extra network request when EISDIR or 545 * EACCES is returned would have to be weighed against the 50% reduction 546 * in network traffic in the other paths. 547 */ 548 if (!(oflags & O_CREAT)) { 549 /* 550 * Check for hashed negative dentry. We have already revalidated 551 * the dentry and it is fine. No need to perform another lookup. 552 */ 553 if (!d_in_lookup(direntry)) 554 return -ENOENT; 555 556 return finish_no_open(file, cifs_lookup(dir, direntry, 0)); 557 } 558 559 xid = get_xid(); 560 561 cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n", 562 dir, direntry, direntry); 563 564 tlink = cifs_sb_tlink(cifs_sb); 565 if (IS_ERR(tlink)) { 566 rc = PTR_ERR(tlink); 567 goto out_free_xid; 568 } 569 570 tcon = tlink_tcon(tlink); 571 572 rc = check_name(direntry, tcon); 573 if (rc) 574 goto out; 575 576 server = tcon->ses->server; 577 578 if (server->ops->new_lease_key) 579 server->ops->new_lease_key(&fid); 580 581 cifs_add_pending_open(&fid, tlink, &open); 582 583 rc = cifs_do_create(dir, direntry, xid, tlink, oflags, mode, 584 &oplock, &fid, &buf, &inode); 585 if (rc) { 586 cifs_del_pending_open(&open); 587 goto out; 588 } 589 590 if (d_in_lookup(direntry)) { 591 alias = d_splice_alias(inode, direntry); 592 if (!IS_ERR_OR_NULL(alias)) 593 direntry = alias; 594 } else { 595 d_instantiate(direntry, inode); 596 } 597 598 if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) 599 file->f_mode |= FMODE_CREATED; 600 601 rc = finish_open(file, direntry, generic_file_open); 602 if (rc) { 603 if (server->ops->close) 604 server->ops->close(xid, tcon, &fid); 605 cifs_del_pending_open(&open); 606 goto out; 607 } 608 609 sbflags = cifs_sb_flags(cifs_sb); 610 if ((file->f_flags & O_DIRECT) && (sbflags & CIFS_MOUNT_STRICT_IO)) { 611 if (sbflags & CIFS_MOUNT_NO_BRL) 612 file->f_op = &cifs_file_direct_nobrl_ops; 613 else 614 file->f_op = &cifs_file_direct_ops; 615 } 616 617 file_info = cifs_new_fileinfo(&fid, file, tlink, oplock, buf.symlink_target); 618 if (file_info == NULL) { 619 if (server->ops->close) 620 server->ops->close(xid, tcon, &fid); 621 cifs_del_pending_open(&open); 622 rc = -ENOMEM; 623 goto out; 624 } 625 626 fscache_use_cookie(cifs_inode_cookie(file_inode(file)), 627 file->f_mode & FMODE_WRITE); 628 629 out: 630 cifs_put_tlink(tlink); 631 out_free_xid: 632 free_xid(xid); 633 cifs_free_open_info(&buf); 634 return rc; 635 } 636 637 /* 638 * Create a CIFS file. 639 * 640 * The initial dentry state is hashed-negative. On success, dentry will become 641 * hashed-positive by calling d_instantiate(). 642 */ 643 int cifs_create(struct mnt_idmap *idmap, struct inode *dir, 644 struct dentry *direntry, umode_t mode) 645 { 646 struct cifs_sb_info *cifs_sb = CIFS_SB(dir); 647 int rc; 648 unsigned int xid = get_xid(); 649 /* 650 * BB below access is probably too much for mknod to request 651 * but we have to do query and setpathinfo so requesting 652 * less could fail (unless we want to request getatr and setatr 653 * permissions (only). At least for POSIX we do not have to 654 * request so much. 655 */ 656 unsigned oflags = O_EXCL | O_CREAT | O_RDWR; 657 struct tcon_link *tlink; 658 struct cifs_tcon *tcon; 659 struct TCP_Server_Info *server; 660 struct inode *inode; 661 struct cifs_fid fid; 662 __u32 oplock; 663 struct cifs_open_info_data buf = {}; 664 665 cifs_dbg(FYI, "cifs_create parent inode = 0x%p name is: %pd and dentry = 0x%p\n", 666 dir, direntry, direntry); 667 668 if (unlikely(cifs_forced_shutdown(cifs_sb))) { 669 rc = smb_EIO(smb_eio_trace_forced_shutdown); 670 goto out_free_xid; 671 } 672 673 tlink = cifs_sb_tlink(cifs_sb); 674 rc = PTR_ERR(tlink); 675 if (IS_ERR(tlink)) 676 goto out_free_xid; 677 678 tcon = tlink_tcon(tlink); 679 server = tcon->ses->server; 680 681 if (server->ops->new_lease_key) 682 server->ops->new_lease_key(&fid); 683 684 rc = cifs_do_create(dir, direntry, xid, tlink, oflags, 685 mode, &oplock, &fid, &buf, &inode); 686 if (!rc) { 687 d_instantiate(direntry, inode); 688 if (server->ops->close) 689 server->ops->close(xid, tcon, &fid); 690 } 691 692 cifs_free_open_info(&buf); 693 cifs_put_tlink(tlink); 694 out_free_xid: 695 free_xid(xid); 696 return rc; 697 } 698 699 int cifs_mknod(struct mnt_idmap *idmap, struct inode *inode, 700 struct dentry *direntry, umode_t mode, dev_t device_number) 701 { 702 int rc = -EPERM; 703 unsigned int xid; 704 struct cifs_sb_info *cifs_sb; 705 struct tcon_link *tlink; 706 struct cifs_tcon *tcon; 707 const char *full_path; 708 void *page; 709 710 if (!old_valid_dev(device_number)) 711 return -EINVAL; 712 713 cifs_sb = CIFS_SB(inode->i_sb); 714 if (unlikely(cifs_forced_shutdown(cifs_sb))) 715 return smb_EIO(smb_eio_trace_forced_shutdown); 716 717 tlink = cifs_sb_tlink(cifs_sb); 718 if (IS_ERR(tlink)) 719 return PTR_ERR(tlink); 720 721 page = alloc_dentry_path(); 722 tcon = tlink_tcon(tlink); 723 xid = get_xid(); 724 725 full_path = build_path_from_dentry(direntry, page); 726 if (IS_ERR(full_path)) { 727 rc = PTR_ERR(full_path); 728 goto mknod_out; 729 } 730 731 trace_smb3_mknod_enter(xid, tcon->tid, tcon->ses->Suid, full_path); 732 733 rc = tcon->ses->server->ops->make_node(xid, inode, direntry, tcon, 734 full_path, mode, 735 device_number); 736 737 mknod_out: 738 if (rc) 739 trace_smb3_mknod_err(xid, tcon->tid, tcon->ses->Suid, rc); 740 else 741 trace_smb3_mknod_done(xid, tcon->tid, tcon->ses->Suid); 742 743 free_dentry_path(page); 744 free_xid(xid); 745 cifs_put_tlink(tlink); 746 return rc; 747 } 748 749 struct dentry * 750 cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, 751 unsigned int flags) 752 { 753 unsigned int xid; 754 int rc = 0; /* to get around spurious gcc warning, set to zero here */ 755 struct cifs_sb_info *cifs_sb; 756 struct tcon_link *tlink; 757 struct cifs_tcon *pTcon; 758 struct inode *newInode = NULL; 759 const char *full_path; 760 void *page; 761 int retry_count = 0; 762 struct dentry *de; 763 764 xid = get_xid(); 765 766 cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n", 767 parent_dir_inode, direntry, direntry); 768 769 /* check whether path exists */ 770 771 cifs_sb = CIFS_SB(parent_dir_inode->i_sb); 772 tlink = cifs_sb_tlink(cifs_sb); 773 if (IS_ERR(tlink)) { 774 de = ERR_CAST(tlink); 775 goto free_xid; 776 } 777 pTcon = tlink_tcon(tlink); 778 779 rc = check_name(direntry, pTcon); 780 if (unlikely(rc)) { 781 de = ERR_PTR(rc); 782 goto put_tlink; 783 } 784 785 /* can not grab the rename sem here since it would 786 deadlock in the cases (beginning of sys_rename itself) 787 in which we already have the sb rename sem */ 788 page = alloc_dentry_path(); 789 full_path = build_path_from_dentry(direntry, page); 790 if (IS_ERR(full_path)) { 791 de = ERR_CAST(full_path); 792 goto free_dentry_path; 793 } 794 795 if (d_really_is_positive(direntry)) { 796 cifs_dbg(FYI, "non-NULL inode in lookup\n"); 797 } else { 798 struct cached_fid *cfid = NULL; 799 800 cifs_dbg(FYI, "NULL inode in lookup\n"); 801 802 /* 803 * We can only rely on negative dentries having the same 804 * spelling as the cached dirent if case insensitivity is 805 * forced on mount. 806 * 807 * XXX: if servers correctly announce Case Sensitivity Search 808 * on GetInfo of FileFSAttributeInformation, then we can take 809 * correct action even if case insensitive is not forced on 810 * mount. 811 */ 812 if (pTcon->nocase && !open_cached_dir_by_dentry(pTcon, direntry->d_parent, &cfid)) { 813 /* 814 * dentry is negative and parent is fully cached: 815 * we can assume file does not exist 816 */ 817 if (cfid->dirents.is_valid) { 818 close_cached_dir(cfid); 819 goto out; 820 } 821 close_cached_dir(cfid); 822 } 823 } 824 cifs_dbg(FYI, "Full path: %s inode = 0x%p\n", 825 full_path, d_inode(direntry)); 826 827 again: 828 if (pTcon->posix_extensions) { 829 rc = smb311_posix_get_inode_info(&newInode, full_path, NULL, 830 parent_dir_inode->i_sb, xid); 831 } else if (pTcon->unix_ext) { 832 rc = cifs_get_inode_info_unix(&newInode, full_path, 833 parent_dir_inode->i_sb, xid); 834 } else { 835 rc = cifs_get_inode_info(&newInode, full_path, NULL, 836 parent_dir_inode->i_sb, xid, NULL); 837 } 838 839 if (rc == 0) { 840 /* since paths are not looked up by component - the parent 841 directories are presumed to be good here */ 842 renew_parental_timestamps(direntry); 843 } else if (rc == -EAGAIN && retry_count++ < 10) { 844 goto again; 845 } else if (rc == -ENOENT) { 846 cifs_set_time(direntry, jiffies); 847 newInode = NULL; 848 } else { 849 if (rc != -EACCES) { 850 cifs_dbg(FYI, "Unexpected lookup error %d\n", rc); 851 /* We special case check for Access Denied - since that 852 is a common return code */ 853 } 854 newInode = ERR_PTR(rc); 855 } 856 857 out: 858 de = d_splice_alias(newInode, direntry); 859 free_dentry_path: 860 free_dentry_path(page); 861 put_tlink: 862 cifs_put_tlink(tlink); 863 free_xid: 864 free_xid(xid); 865 return de; 866 } 867 868 static int 869 cifs_d_revalidate(struct inode *dir, const struct qstr *name, 870 struct dentry *direntry, unsigned int flags) 871 { 872 if (flags & LOOKUP_RCU) 873 return -ECHILD; 874 875 if (d_really_is_positive(direntry)) { 876 int rc; 877 struct inode *inode = d_inode(direntry); 878 879 if ((flags & LOOKUP_REVAL) && !CIFS_CACHE_READ(CIFS_I(inode))) 880 CIFS_I(inode)->time = 0; /* force reval */ 881 882 rc = cifs_revalidate_dentry(direntry); 883 if (rc) { 884 cifs_dbg(FYI, "cifs_revalidate_dentry failed with rc=%d", rc); 885 switch (rc) { 886 case -ENOENT: 887 case -ESTALE: 888 /* 889 * Those errors mean the dentry is invalid 890 * (file was deleted or recreated) 891 */ 892 return 0; 893 default: 894 /* 895 * Otherwise some unexpected error happened 896 * report it as-is to VFS layer 897 */ 898 return rc; 899 } 900 } 901 else { 902 /* 903 * If the inode wasn't known to be a dfs entry when 904 * the dentry was instantiated, such as when created 905 * via ->readdir(), it needs to be set now since the 906 * attributes will have been updated by 907 * cifs_revalidate_dentry(). 908 */ 909 if (IS_AUTOMOUNT(inode) && 910 !(direntry->d_flags & DCACHE_NEED_AUTOMOUNT)) { 911 spin_lock(&direntry->d_lock); 912 direntry->d_flags |= DCACHE_NEED_AUTOMOUNT; 913 spin_unlock(&direntry->d_lock); 914 } 915 916 return 1; 917 } 918 } else { 919 struct cifs_sb_info *cifs_sb = CIFS_SB(dir->i_sb); 920 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); 921 struct cached_fid *cfid; 922 923 if (!open_cached_dir_by_dentry(tcon, direntry->d_parent, &cfid)) { 924 /* 925 * dentry is negative and parent is fully cached: 926 * we can assume file does not exist 927 */ 928 if (cfid->dirents.is_valid) { 929 close_cached_dir(cfid); 930 return 1; 931 } 932 close_cached_dir(cfid); 933 } 934 } 935 936 /* 937 * This may be nfsd (or something), anyway, we can't see the 938 * intent of this. So, since this can be for creation, drop it. 939 */ 940 if (!flags) 941 return 0; 942 943 /* 944 * Drop the negative dentry, in order to make sure to use the 945 * case sensitive name which is specified by user if this is 946 * for creation. 947 */ 948 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 949 return 0; 950 951 if (time_after(jiffies, cifs_get_time(direntry) + HZ) || !lookupCacheEnabled) 952 return 0; 953 954 return 1; 955 } 956 957 /* static int cifs_d_delete(struct dentry *direntry) 958 { 959 int rc = 0; 960 961 cifs_dbg(FYI, "In cifs d_delete, name = %pd\n", direntry); 962 963 return rc; 964 } */ 965 966 const struct dentry_operations cifs_dentry_ops = { 967 .d_revalidate = cifs_d_revalidate, 968 .d_automount = cifs_d_automount, 969 /* d_delete: cifs_d_delete, */ /* not needed except for debugging */ 970 }; 971 972 static int cifs_ci_hash(const struct dentry *dentry, struct qstr *q) 973 { 974 struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls; 975 unsigned long hash; 976 wchar_t c; 977 int i, charlen; 978 979 hash = init_name_hash(dentry); 980 for (i = 0; i < q->len; i += charlen) { 981 charlen = codepage->char2uni(&q->name[i], q->len - i, &c); 982 /* error out if we can't convert the character */ 983 if (unlikely(charlen < 0)) 984 return charlen; 985 hash = partial_name_hash(cifs_toupper(c), hash); 986 } 987 q->hash = end_name_hash(hash); 988 989 return 0; 990 } 991 992 static int cifs_ci_compare(const struct dentry *dentry, 993 unsigned int len, const char *str, const struct qstr *name) 994 { 995 struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls; 996 wchar_t c1, c2; 997 int i, l1, l2; 998 999 /* 1000 * We make the assumption here that uppercase characters in the local 1001 * codepage are always the same length as their lowercase counterparts. 1002 * 1003 * If that's ever not the case, then this will fail to match it. 1004 */ 1005 if (name->len != len) 1006 return 1; 1007 1008 for (i = 0; i < len; i += l1) { 1009 /* Convert characters in both strings to UTF-16. */ 1010 l1 = codepage->char2uni(&str[i], len - i, &c1); 1011 l2 = codepage->char2uni(&name->name[i], name->len - i, &c2); 1012 1013 /* 1014 * If we can't convert either character, just declare it to 1015 * be 1 byte long and compare the original byte. 1016 */ 1017 if (unlikely(l1 < 0 && l2 < 0)) { 1018 if (str[i] != name->name[i]) 1019 return 1; 1020 l1 = 1; 1021 continue; 1022 } 1023 1024 /* 1025 * Here, we again ass|u|me that upper/lowercase versions of 1026 * a character are the same length in the local NLS. 1027 */ 1028 if (l1 != l2) 1029 return 1; 1030 1031 /* Now compare uppercase versions of these characters */ 1032 if (cifs_toupper(c1) != cifs_toupper(c2)) 1033 return 1; 1034 } 1035 1036 return 0; 1037 } 1038 1039 static int set_tmpfile_attr(const unsigned int xid, unsigned int oflags, 1040 struct inode *inode, const char *full_path, 1041 struct TCP_Server_Info *server) 1042 { 1043 struct cifsInodeInfo *cinode = CIFS_I(inode); 1044 FILE_BASIC_INFO fi; 1045 1046 cinode->cifsAttrs |= ATTR_HIDDEN; 1047 if (oflags & O_EXCL) 1048 cinode->cifsAttrs |= ATTR_TEMPORARY; 1049 1050 fi = (FILE_BASIC_INFO) { 1051 .Attributes = cpu_to_le32(cinode->cifsAttrs), 1052 }; 1053 return server->ops->set_file_info(inode, full_path, &fi, xid); 1054 } 1055 1056 /* 1057 * Create a hidden temporary CIFS file with delete-on-close bit set. 1058 * 1059 * The initial dentry state is unhashed-negative. On success, dentry will 1060 * become unhashed-positive by calling d_instantiate(). 1061 */ 1062 int cifs_tmpfile(struct mnt_idmap *idmap, struct inode *dir, 1063 struct file *file, umode_t mode) 1064 { 1065 struct dentry *dentry = file->f_path.dentry; 1066 struct cifs_sb_info *cifs_sb = CIFS_SB(dir); 1067 size_t namesize = CIFS_TMPNAME_LEN + 1; 1068 char *path __free(kfree) = NULL, *name; 1069 unsigned int oflags = file->f_flags; 1070 int retries = 0, max_retries = 16; 1071 struct TCP_Server_Info *server; 1072 struct cifs_pending_open open; 1073 struct cifsFileInfo *cfile; 1074 struct cifs_fid fid = {}; 1075 struct tcon_link *tlink; 1076 struct cifs_tcon *tcon; 1077 unsigned int sbflags; 1078 struct inode *inode; 1079 unsigned int xid; 1080 __u32 oplock; 1081 int namelen; 1082 int rc; 1083 1084 if (unlikely(cifs_forced_shutdown(cifs_sb))) 1085 return smb_EIO(smb_eio_trace_forced_shutdown); 1086 1087 tlink = cifs_sb_tlink(cifs_sb); 1088 if (IS_ERR(tlink)) 1089 return PTR_ERR(tlink); 1090 tcon = tlink_tcon(tlink); 1091 server = tcon->ses->server; 1092 1093 xid = get_xid(); 1094 1095 if (server->vals->protocol_id < SMB20_PROT_ID) { 1096 cifs_dbg(VFS | ONCE, "O_TMPFILE is supported only in SMB2+\n"); 1097 rc = -EOPNOTSUPP; 1098 goto out; 1099 } 1100 1101 if (server->ops->new_lease_key) 1102 server->ops->new_lease_key(&fid); 1103 cifs_add_pending_open(&fid, tlink, &open); 1104 1105 path = alloc_parent_path(dentry, namesize - 1); 1106 if (IS_ERR(path)) { 1107 cifs_del_pending_open(&open); 1108 rc = PTR_ERR(path); 1109 path = NULL; 1110 goto out; 1111 } 1112 1113 name = path + strlen(path); 1114 do { 1115 /* Append tmpfile name to @path */ 1116 namelen = scnprintf(name, namesize, CIFS_TMPNAME_PREFIX "%x", 1117 atomic_inc_return(&cifs_tmpcounter)); 1118 rc = __cifs_do_create(dir, dentry, path, xid, tlink, oflags, 1119 mode, &oplock, &fid, NULL, &inode); 1120 if (!rc) { 1121 rc = d_mark_tmpfile_name(file, &QSTR_LEN(name, namelen)); 1122 if (rc) { 1123 cifs_dbg(VFS | ONCE, "%s: failed to set filename in dentry: %d\n", 1124 __func__, rc); 1125 rc = -EISDIR; 1126 iput(inode); 1127 goto err_open; 1128 } 1129 set_nlink(inode, 0); 1130 mark_inode_dirty(inode); 1131 d_instantiate(dentry, inode); 1132 break; 1133 } 1134 } while (unlikely(rc == -EEXIST) && ++retries < max_retries); 1135 1136 if (rc) { 1137 if (rc == -ENOENT) 1138 rc = -EOPNOTSUPP; 1139 cifs_del_pending_open(&open); 1140 goto out; 1141 } 1142 1143 rc = finish_open(file, dentry, generic_file_open); 1144 if (rc) 1145 goto err_open; 1146 1147 sbflags = cifs_sb_flags(cifs_sb); 1148 if ((file->f_flags & O_DIRECT) && (sbflags & CIFS_MOUNT_STRICT_IO)) { 1149 if (sbflags & CIFS_MOUNT_NO_BRL) 1150 file->f_op = &cifs_file_direct_nobrl_ops; 1151 else 1152 file->f_op = &cifs_file_direct_ops; 1153 } 1154 1155 cfile = cifs_new_fileinfo(&fid, file, tlink, oplock, NULL); 1156 if (!cfile) { 1157 rc = -ENOMEM; 1158 goto err_open; 1159 } 1160 1161 rc = set_tmpfile_attr(xid, oflags, inode, path, server); 1162 if (rc) 1163 goto out; 1164 1165 fscache_use_cookie(cifs_inode_cookie(file_inode(file)), 1166 file->f_mode & FMODE_WRITE); 1167 out: 1168 cifs_put_tlink(tlink); 1169 free_xid(xid); 1170 return rc; 1171 err_open: 1172 cifs_del_pending_open(&open); 1173 if (server->ops->close) 1174 server->ops->close(xid, tcon, &fid); 1175 goto out; 1176 } 1177 1178 char *cifs_silly_fullpath(struct dentry *dentry) 1179 { 1180 unsigned char name[CIFS_SILLYNAME_LEN + 1]; 1181 int retries = 0, max_retries = 16; 1182 size_t namesize = sizeof(name); 1183 struct dentry *sdentry = NULL; 1184 char *path; 1185 1186 do { 1187 dput(sdentry); 1188 scnprintf(name, namesize, CIFS_SILLYNAME_PREFIX "%x", 1189 atomic_inc_return(&cifs_sillycounter)); 1190 sdentry = lookup_noperm(&QSTR(name), dentry->d_parent); 1191 if (IS_ERR(sdentry)) 1192 return ERR_CAST(sdentry); 1193 if (d_is_negative(sdentry)) { 1194 dput(sdentry); 1195 path = alloc_parent_path(dentry, CIFS_SILLYNAME_LEN); 1196 if (!IS_ERR(path)) 1197 strcat(path, name); 1198 return path; 1199 } 1200 } while (++retries < max_retries); 1201 dput(sdentry); 1202 return ERR_PTR(-EBUSY); 1203 } 1204 1205 const struct dentry_operations cifs_ci_dentry_ops = { 1206 .d_revalidate = cifs_d_revalidate, 1207 .d_hash = cifs_ci_hash, 1208 .d_compare = cifs_ci_compare, 1209 .d_automount = cifs_d_automount, 1210 }; 1211