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