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