1 // SPDX-License-Identifier: LGPL-2.1 2 /* 3 * 4 * Directory search handling 5 * 6 * Copyright (C) International Business Machines Corp., 2004, 2008 7 * Copyright (C) Red Hat, Inc., 2011 8 * Author(s): Steve French (sfrench@us.ibm.com) 9 * 10 */ 11 #include <linux/fs.h> 12 #include <linux/pagemap.h> 13 #include <linux/slab.h> 14 #include <linux/stat.h> 15 #include "cifspdu.h" 16 #include "cifsglob.h" 17 #include "cifsproto.h" 18 #include "cifs_unicode.h" 19 #include "cifs_debug.h" 20 #include "cifs_fs_sb.h" 21 #include "cifsfs.h" 22 #include "smb2proto.h" 23 #include "fs_context.h" 24 #include "cached_dir.h" 25 26 /* 27 * To be safe - for UCS to UTF-8 with strings loaded with the rare long 28 * characters alloc more to account for such multibyte target UTF-8 29 * characters. 30 */ 31 #define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2) 32 33 #ifdef CONFIG_CIFS_DEBUG2 34 static void dump_cifs_file_struct(struct file *file, char *label) 35 { 36 struct cifsFileInfo *cf; 37 38 if (file) { 39 cf = file->private_data; 40 if (cf == NULL) { 41 cifs_dbg(FYI, "empty cifs private file data\n"); 42 return; 43 } 44 if (cf->invalidHandle) 45 cifs_dbg(FYI, "Invalid handle\n"); 46 if (cf->srch_inf.endOfSearch) 47 cifs_dbg(FYI, "end of search\n"); 48 if (cf->srch_inf.emptyDir) 49 cifs_dbg(FYI, "empty dir\n"); 50 } 51 } 52 #else 53 static inline void dump_cifs_file_struct(struct file *file, char *label) 54 { 55 } 56 #endif /* DEBUG2 */ 57 58 /* 59 * Match a reparse point inode if reparse tag and ctime haven't changed. 60 * 61 * Windows Server updates ctime of reparse points when their data have changed. 62 * The server doesn't allow changing reparse tags from existing reparse points, 63 * though it's worth checking. 64 */ 65 static inline bool reparse_inode_match(struct inode *inode, 66 struct cifs_fattr *fattr) 67 { 68 struct timespec64 ctime = inode_get_ctime(inode); 69 70 return (CIFS_I(inode)->cifsAttrs & ATTR_REPARSE) && 71 CIFS_I(inode)->reparse_tag == fattr->cf_cifstag && 72 timespec64_equal(&ctime, &fattr->cf_ctime); 73 } 74 75 /* 76 * Attempt to preload the dcache with the results from the FIND_FIRST/NEXT 77 * 78 * Find the dentry that matches "name". If there isn't one, create one. If it's 79 * a negative dentry or the uniqueid or filetype(mode) changed, 80 * then drop it and recreate it. 81 */ 82 static void 83 cifs_prime_dcache(struct dentry *parent, struct qstr *name, 84 struct cifs_fattr *fattr) 85 { 86 struct dentry *dentry, *alias; 87 struct inode *inode; 88 struct super_block *sb = parent->d_sb; 89 struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 90 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 91 int rc; 92 93 cifs_dbg(FYI, "%s: for %s\n", __func__, name->name); 94 95 dentry = d_hash_and_lookup(parent, name); 96 if (!dentry) { 97 /* 98 * If we know that the inode will need to be revalidated 99 * immediately, then don't create a new dentry for it. 100 * We'll end up doing an on the wire call either way and 101 * this spares us an invalidation. 102 */ 103 retry: 104 if ((fattr->cf_cifsattrs & ATTR_REPARSE) || 105 (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)) 106 return; 107 108 dentry = d_alloc_parallel(parent, name, &wq); 109 } 110 if (IS_ERR(dentry)) 111 return; 112 if (!d_in_lookup(dentry)) { 113 inode = d_inode(dentry); 114 if (inode) { 115 if (d_mountpoint(dentry)) { 116 dput(dentry); 117 return; 118 } 119 /* 120 * If we're generating inode numbers, then we don't 121 * want to clobber the existing one with the one that 122 * the readdir code created. 123 */ 124 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) 125 fattr->cf_uniqueid = CIFS_I(inode)->uniqueid; 126 127 /* 128 * Update inode in place if both i_ino and i_mode didn't 129 * change. 130 */ 131 if (CIFS_I(inode)->uniqueid == fattr->cf_uniqueid) { 132 /* 133 * Query dir responses don't provide enough 134 * information about reparse points other than 135 * their reparse tags. Save an invalidation by 136 * not clobbering some existing attributes when 137 * reparse tag and ctime haven't changed. 138 */ 139 rc = 0; 140 if (fattr->cf_cifsattrs & ATTR_REPARSE) { 141 if (likely(reparse_inode_match(inode, fattr))) { 142 fattr->cf_mode = inode->i_mode; 143 fattr->cf_rdev = inode->i_rdev; 144 fattr->cf_eof = CIFS_I(inode)->server_eof; 145 fattr->cf_symlink_target = NULL; 146 } else { 147 CIFS_I(inode)->time = 0; 148 rc = -ESTALE; 149 } 150 } 151 if (!rc && !cifs_fattr_to_inode(inode, fattr)) { 152 dput(dentry); 153 return; 154 } 155 } 156 } 157 d_invalidate(dentry); 158 dput(dentry); 159 goto retry; 160 } else { 161 inode = cifs_iget(sb, fattr); 162 if (!inode) 163 inode = ERR_PTR(-ENOMEM); 164 alias = d_splice_alias(inode, dentry); 165 d_lookup_done(dentry); 166 if (alias && !IS_ERR(alias)) 167 dput(alias); 168 } 169 dput(dentry); 170 } 171 172 static void 173 cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb) 174 { 175 struct cifs_open_info_data data = { 176 .reparse = { .tag = fattr->cf_cifstag, }, 177 }; 178 179 fattr->cf_uid = cifs_sb->ctx->linux_uid; 180 fattr->cf_gid = cifs_sb->ctx->linux_gid; 181 182 /* 183 * The IO_REPARSE_TAG_LX_ tags originally were used by WSL but they 184 * are preferred by the Linux client in some cases since, unlike 185 * the NFS reparse tag (or EAs), they don't require an extra query 186 * to determine which type of special file they represent. 187 * TODO: go through all documented reparse tags to see if we can 188 * reasonably map some of them to directories vs. files vs. symlinks 189 */ 190 if ((fattr->cf_cifsattrs & ATTR_REPARSE) && 191 cifs_reparse_point_to_fattr(cifs_sb, fattr, &data)) 192 goto out_reparse; 193 194 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) { 195 fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode; 196 fattr->cf_dtype = DT_DIR; 197 } else { 198 fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode; 199 fattr->cf_dtype = DT_REG; 200 } 201 202 out_reparse: 203 /* non-unix readdir doesn't provide nlink */ 204 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK; 205 206 if (fattr->cf_cifsattrs & ATTR_READONLY) 207 fattr->cf_mode &= ~S_IWUGO; 208 209 /* 210 * We of course don't get ACL info in FIND_FIRST/NEXT results, so 211 * mark it for revalidation so that "ls -l" will look right. It might 212 * be super-slow, but if we don't do this then the ownership of files 213 * may look wrong since the inodes may not have timed out by the time 214 * "ls" does a stat() call on them. 215 */ 216 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) || 217 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) 218 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL; 219 220 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL && 221 fattr->cf_cifsattrs & ATTR_SYSTEM) { 222 if (fattr->cf_eof == 0) { 223 fattr->cf_mode &= ~S_IFMT; 224 fattr->cf_mode |= S_IFIFO; 225 fattr->cf_dtype = DT_FIFO; 226 } else { 227 /* 228 * trying to get the type and mode via SFU can be slow, 229 * so just call those regular files for now, and mark 230 * for reval 231 */ 232 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL; 233 } 234 } 235 } 236 237 /* Fill a cifs_fattr struct with info from SMB_FIND_FILE_POSIX_INFO. */ 238 static void 239 cifs_posix_to_fattr(struct cifs_fattr *fattr, struct smb2_posix_info *info, 240 struct cifs_sb_info *cifs_sb) 241 { 242 struct smb2_posix_info_parsed parsed; 243 244 posix_info_parse(info, NULL, &parsed); 245 246 memset(fattr, 0, sizeof(*fattr)); 247 fattr->cf_uniqueid = le64_to_cpu(info->Inode); 248 fattr->cf_bytes = le64_to_cpu(info->AllocationSize); 249 fattr->cf_eof = le64_to_cpu(info->EndOfFile); 250 251 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime); 252 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime); 253 fattr->cf_ctime = cifs_NTtimeToUnix(info->CreationTime); 254 255 fattr->cf_nlink = le32_to_cpu(info->HardLinks); 256 fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes); 257 258 /* 259 * Since we set the inode type below we need to mask off 260 * to avoid strange results if bits set above. 261 * XXX: why not make server&client use the type bits? 262 */ 263 fattr->cf_mode = le32_to_cpu(info->Mode) & ~S_IFMT; 264 265 cifs_dbg(FYI, "posix fattr: dev %d, reparse %d, mode %o\n", 266 le32_to_cpu(info->DeviceId), 267 le32_to_cpu(info->ReparseTag), 268 le32_to_cpu(info->Mode)); 269 270 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) { 271 fattr->cf_mode |= S_IFDIR; 272 fattr->cf_dtype = DT_DIR; 273 } else { 274 /* 275 * mark anything that is not a dir as regular 276 * file. special files should have the REPARSE 277 * attribute and will be marked as needing revaluation 278 */ 279 fattr->cf_mode |= S_IFREG; 280 fattr->cf_dtype = DT_REG; 281 } 282 283 sid_to_id(cifs_sb, &parsed.owner, fattr, SIDOWNER); 284 sid_to_id(cifs_sb, &parsed.group, fattr, SIDGROUP); 285 } 286 287 static void __dir_info_to_fattr(struct cifs_fattr *fattr, const void *info) 288 { 289 const FILE_DIRECTORY_INFO *fi = info; 290 291 memset(fattr, 0, sizeof(*fattr)); 292 fattr->cf_cifsattrs = le32_to_cpu(fi->ExtFileAttributes); 293 fattr->cf_eof = le64_to_cpu(fi->EndOfFile); 294 fattr->cf_bytes = le64_to_cpu(fi->AllocationSize); 295 fattr->cf_createtime = le64_to_cpu(fi->CreationTime); 296 fattr->cf_atime = cifs_NTtimeToUnix(fi->LastAccessTime); 297 fattr->cf_ctime = cifs_NTtimeToUnix(fi->ChangeTime); 298 fattr->cf_mtime = cifs_NTtimeToUnix(fi->LastWriteTime); 299 } 300 301 void 302 cifs_dir_info_to_fattr(struct cifs_fattr *fattr, FILE_DIRECTORY_INFO *info, 303 struct cifs_sb_info *cifs_sb) 304 { 305 __dir_info_to_fattr(fattr, info); 306 cifs_fill_common_info(fattr, cifs_sb); 307 } 308 309 static void cifs_fulldir_info_to_fattr(struct cifs_fattr *fattr, 310 SEARCH_ID_FULL_DIR_INFO *info, 311 struct cifs_sb_info *cifs_sb) 312 { 313 __dir_info_to_fattr(fattr, info); 314 315 /* See MS-FSCC 2.4.19 FileIdFullDirectoryInformation */ 316 if (fattr->cf_cifsattrs & ATTR_REPARSE) 317 fattr->cf_cifstag = le32_to_cpu(info->EaSize); 318 cifs_fill_common_info(fattr, cifs_sb); 319 } 320 321 static void 322 cifs_std_info_to_fattr(struct cifs_fattr *fattr, FIND_FILE_STANDARD_INFO *info, 323 struct cifs_sb_info *cifs_sb) 324 { 325 int offset = cifs_sb_master_tcon(cifs_sb)->ses->server->timeAdj; 326 327 memset(fattr, 0, sizeof(*fattr)); 328 fattr->cf_atime = cnvrtDosUnixTm(info->LastAccessDate, 329 info->LastAccessTime, offset); 330 fattr->cf_ctime = cnvrtDosUnixTm(info->LastWriteDate, 331 info->LastWriteTime, offset); 332 fattr->cf_mtime = cnvrtDosUnixTm(info->LastWriteDate, 333 info->LastWriteTime, offset); 334 335 fattr->cf_cifsattrs = le16_to_cpu(info->Attributes); 336 fattr->cf_bytes = le32_to_cpu(info->AllocationSize); 337 fattr->cf_eof = le32_to_cpu(info->DataSize); 338 339 cifs_fill_common_info(fattr, cifs_sb); 340 } 341 342 static int 343 _initiate_cifs_search(const unsigned int xid, struct file *file, 344 const char *full_path) 345 { 346 __u16 search_flags; 347 int rc = 0; 348 struct cifsFileInfo *cifsFile; 349 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file); 350 struct tcon_link *tlink = NULL; 351 struct cifs_tcon *tcon; 352 struct TCP_Server_Info *server; 353 354 if (file->private_data == NULL) { 355 tlink = cifs_sb_tlink(cifs_sb); 356 if (IS_ERR(tlink)) 357 return PTR_ERR(tlink); 358 359 cifsFile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL); 360 if (cifsFile == NULL) { 361 rc = -ENOMEM; 362 goto error_exit; 363 } 364 spin_lock_init(&cifsFile->file_info_lock); 365 file->private_data = cifsFile; 366 cifsFile->tlink = cifs_get_tlink(tlink); 367 tcon = tlink_tcon(tlink); 368 } else { 369 cifsFile = file->private_data; 370 tcon = tlink_tcon(cifsFile->tlink); 371 } 372 373 server = tcon->ses->server; 374 375 if (!server->ops->query_dir_first) { 376 rc = -ENOSYS; 377 goto error_exit; 378 } 379 380 cifsFile->invalidHandle = true; 381 cifsFile->srch_inf.endOfSearch = false; 382 383 cifs_dbg(FYI, "Full path: %s start at: %lld\n", full_path, file->f_pos); 384 385 ffirst_retry: 386 /* test for Unix extensions */ 387 /* but now check for them on the share/mount not on the SMB session */ 388 /* if (cap_unix(tcon->ses) { */ 389 if (tcon->unix_ext) 390 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX; 391 else if (tcon->posix_extensions) 392 cifsFile->srch_inf.info_level = SMB_FIND_FILE_POSIX_INFO; 393 else if ((tcon->ses->capabilities & 394 tcon->ses->server->vals->cap_nt_find) == 0) { 395 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD; 396 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) { 397 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO; 398 } else /* not srvinos - BB fixme add check for backlevel? */ { 399 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO; 400 } 401 402 search_flags = CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME; 403 if (backup_cred(cifs_sb)) 404 search_flags |= CIFS_SEARCH_BACKUP_SEARCH; 405 406 rc = server->ops->query_dir_first(xid, tcon, full_path, cifs_sb, 407 &cifsFile->fid, search_flags, 408 &cifsFile->srch_inf); 409 410 if (rc == 0) { 411 cifsFile->invalidHandle = false; 412 } else if ((rc == -EOPNOTSUPP) && 413 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) { 414 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM; 415 goto ffirst_retry; 416 } 417 error_exit: 418 cifs_put_tlink(tlink); 419 return rc; 420 } 421 422 static int 423 initiate_cifs_search(const unsigned int xid, struct file *file, 424 const char *full_path) 425 { 426 int rc, retry_count = 0; 427 428 do { 429 rc = _initiate_cifs_search(xid, file, full_path); 430 /* 431 * If we don't have enough credits to start reading the 432 * directory just try again after short wait. 433 */ 434 if (rc != -EDEADLK) 435 break; 436 437 usleep_range(512, 2048); 438 } while (retry_count++ < 5); 439 440 return rc; 441 } 442 443 /* return length of unicode string in bytes */ 444 static int cifs_unicode_bytelen(const char *str) 445 { 446 int len; 447 const __le16 *ustr = (const __le16 *)str; 448 449 for (len = 0; len <= PATH_MAX; len++) { 450 if (ustr[len] == 0) 451 return len << 1; 452 } 453 cifs_dbg(FYI, "Unicode string longer than PATH_MAX found\n"); 454 return len << 1; 455 } 456 457 static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level) 458 { 459 char *new_entry; 460 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry; 461 462 if (level == SMB_FIND_FILE_INFO_STANDARD) { 463 FIND_FILE_STANDARD_INFO *pfData; 464 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo; 465 466 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) + 1 + 467 pfData->FileNameLength; 468 } else { 469 u32 next_offset = le32_to_cpu(pDirInfo->NextEntryOffset); 470 471 if (old_entry + next_offset < old_entry) { 472 cifs_dbg(VFS, "Invalid offset %u\n", next_offset); 473 return NULL; 474 } 475 new_entry = old_entry + next_offset; 476 } 477 cifs_dbg(FYI, "new entry %p old entry %p\n", new_entry, old_entry); 478 /* validate that new_entry is not past end of SMB */ 479 if (new_entry >= end_of_smb) { 480 cifs_dbg(VFS, "search entry %p began after end of SMB %p old entry %p\n", 481 new_entry, end_of_smb, old_entry); 482 return NULL; 483 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) && 484 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) + 1 > end_of_smb)) 485 || ((level != SMB_FIND_FILE_INFO_STANDARD) && 486 (new_entry + sizeof(FILE_DIRECTORY_INFO) + 1 > end_of_smb))) { 487 cifs_dbg(VFS, "search entry %p extends after end of SMB %p\n", 488 new_entry, end_of_smb); 489 return NULL; 490 } else 491 return new_entry; 492 493 } 494 495 struct cifs_dirent { 496 const char *name; 497 size_t namelen; 498 u32 resume_key; 499 u64 ino; 500 }; 501 502 static void cifs_fill_dirent_posix(struct cifs_dirent *de, 503 const struct smb2_posix_info *info) 504 { 505 struct smb2_posix_info_parsed parsed; 506 507 /* payload should have already been checked at this point */ 508 if (posix_info_parse(info, NULL, &parsed) < 0) { 509 cifs_dbg(VFS, "Invalid POSIX info payload\n"); 510 return; 511 } 512 513 de->name = parsed.name; 514 de->namelen = parsed.name_len; 515 de->resume_key = info->Ignored; 516 de->ino = le64_to_cpu(info->Inode); 517 } 518 519 static void cifs_fill_dirent_unix(struct cifs_dirent *de, 520 const FILE_UNIX_INFO *info, bool is_unicode) 521 { 522 de->name = &info->FileName[0]; 523 if (is_unicode) 524 de->namelen = cifs_unicode_bytelen(de->name); 525 else 526 de->namelen = strnlen(de->name, PATH_MAX); 527 de->resume_key = info->ResumeKey; 528 de->ino = le64_to_cpu(info->basic.UniqueId); 529 } 530 531 static void cifs_fill_dirent_dir(struct cifs_dirent *de, 532 const FILE_DIRECTORY_INFO *info) 533 { 534 de->name = &info->FileName[0]; 535 de->namelen = le32_to_cpu(info->FileNameLength); 536 de->resume_key = info->FileIndex; 537 } 538 539 static void cifs_fill_dirent_full(struct cifs_dirent *de, 540 const FILE_FULL_DIRECTORY_INFO *info) 541 { 542 de->name = &info->FileName[0]; 543 de->namelen = le32_to_cpu(info->FileNameLength); 544 de->resume_key = info->FileIndex; 545 } 546 547 static void cifs_fill_dirent_search(struct cifs_dirent *de, 548 const SEARCH_ID_FULL_DIR_INFO *info) 549 { 550 de->name = &info->FileName[0]; 551 de->namelen = le32_to_cpu(info->FileNameLength); 552 de->resume_key = info->FileIndex; 553 de->ino = le64_to_cpu(info->UniqueId); 554 } 555 556 static void cifs_fill_dirent_both(struct cifs_dirent *de, 557 const FILE_BOTH_DIRECTORY_INFO *info) 558 { 559 de->name = &info->FileName[0]; 560 de->namelen = le32_to_cpu(info->FileNameLength); 561 de->resume_key = info->FileIndex; 562 } 563 564 static void cifs_fill_dirent_std(struct cifs_dirent *de, 565 const FIND_FILE_STANDARD_INFO *info) 566 { 567 de->name = &info->FileName[0]; 568 /* one byte length, no endianess conversion */ 569 de->namelen = info->FileNameLength; 570 de->resume_key = info->ResumeKey; 571 } 572 573 static int cifs_fill_dirent(struct cifs_dirent *de, const void *info, 574 u16 level, bool is_unicode) 575 { 576 memset(de, 0, sizeof(*de)); 577 578 switch (level) { 579 case SMB_FIND_FILE_POSIX_INFO: 580 cifs_fill_dirent_posix(de, info); 581 break; 582 case SMB_FIND_FILE_UNIX: 583 cifs_fill_dirent_unix(de, info, is_unicode); 584 break; 585 case SMB_FIND_FILE_DIRECTORY_INFO: 586 cifs_fill_dirent_dir(de, info); 587 break; 588 case SMB_FIND_FILE_FULL_DIRECTORY_INFO: 589 cifs_fill_dirent_full(de, info); 590 break; 591 case SMB_FIND_FILE_ID_FULL_DIR_INFO: 592 cifs_fill_dirent_search(de, info); 593 break; 594 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO: 595 cifs_fill_dirent_both(de, info); 596 break; 597 case SMB_FIND_FILE_INFO_STANDARD: 598 cifs_fill_dirent_std(de, info); 599 break; 600 default: 601 cifs_dbg(FYI, "Unknown findfirst level %d\n", level); 602 return -EINVAL; 603 } 604 605 return 0; 606 } 607 608 #define UNICODE_DOT cpu_to_le16(0x2e) 609 610 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */ 611 static int cifs_entry_is_dot(struct cifs_dirent *de, bool is_unicode) 612 { 613 int rc = 0; 614 615 if (!de->name) 616 return 0; 617 618 if (is_unicode) { 619 __le16 *ufilename = (__le16 *)de->name; 620 if (de->namelen == 2) { 621 /* check for . */ 622 if (ufilename[0] == UNICODE_DOT) 623 rc = 1; 624 } else if (de->namelen == 4) { 625 /* check for .. */ 626 if (ufilename[0] == UNICODE_DOT && 627 ufilename[1] == UNICODE_DOT) 628 rc = 2; 629 } 630 } else /* ASCII */ { 631 if (de->namelen == 1) { 632 if (de->name[0] == '.') 633 rc = 1; 634 } else if (de->namelen == 2) { 635 if (de->name[0] == '.' && de->name[1] == '.') 636 rc = 2; 637 } 638 } 639 640 return rc; 641 } 642 643 /* Check if directory that we are searching has changed so we can decide 644 whether we can use the cached search results from the previous search */ 645 static int is_dir_changed(struct file *file) 646 { 647 struct inode *inode = file_inode(file); 648 struct cifsInodeInfo *cifs_inode_info = CIFS_I(inode); 649 650 if (cifs_inode_info->time == 0) 651 return 1; /* directory was changed, e.g. unlink or new file */ 652 else 653 return 0; 654 655 } 656 657 static int cifs_save_resume_key(const char *current_entry, 658 struct cifsFileInfo *file_info) 659 { 660 struct cifs_dirent de; 661 int rc; 662 663 rc = cifs_fill_dirent(&de, current_entry, file_info->srch_inf.info_level, 664 file_info->srch_inf.unicode); 665 if (!rc) { 666 file_info->srch_inf.presume_name = de.name; 667 file_info->srch_inf.resume_name_len = de.namelen; 668 file_info->srch_inf.resume_key = de.resume_key; 669 } 670 return rc; 671 } 672 673 /* 674 * Find the corresponding entry in the search. Note that the SMB server returns 675 * search entries for . and .. which complicates logic here if we choose to 676 * parse for them and we do not assume that they are located in the findfirst 677 * return buffer. We start counting in the buffer with entry 2 and increment for 678 * every entry (do not increment for . or .. entry). 679 */ 680 static int 681 find_cifs_entry(const unsigned int xid, struct cifs_tcon *tcon, loff_t pos, 682 struct file *file, const char *full_path, 683 char **current_entry, int *num_to_ret) 684 { 685 __u16 search_flags; 686 int rc = 0; 687 int pos_in_buf = 0; 688 loff_t first_entry_in_buffer; 689 loff_t index_to_find = pos; 690 struct cifsFileInfo *cfile = file->private_data; 691 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file); 692 struct TCP_Server_Info *server = tcon->ses->server; 693 /* check if index in the buffer */ 694 695 if (!server->ops->query_dir_first || !server->ops->query_dir_next) 696 return -ENOSYS; 697 698 if ((cfile == NULL) || (current_entry == NULL) || (num_to_ret == NULL)) 699 return -ENOENT; 700 701 *current_entry = NULL; 702 first_entry_in_buffer = cfile->srch_inf.index_of_last_entry - 703 cfile->srch_inf.entries_in_buffer; 704 705 /* 706 * If first entry in buf is zero then is first buffer 707 * in search response data which means it is likely . and .. 708 * will be in this buffer, although some servers do not return 709 * . and .. for the root of a drive and for those we need 710 * to start two entries earlier. 711 */ 712 713 dump_cifs_file_struct(file, "In fce "); 714 if (((index_to_find < cfile->srch_inf.index_of_last_entry) && 715 is_dir_changed(file)) || (index_to_find < first_entry_in_buffer)) { 716 /* close and restart search */ 717 cifs_dbg(FYI, "search backing up - close and restart search\n"); 718 spin_lock(&cfile->file_info_lock); 719 if (server->ops->dir_needs_close(cfile)) { 720 cfile->invalidHandle = true; 721 spin_unlock(&cfile->file_info_lock); 722 if (server->ops->close_dir) 723 server->ops->close_dir(xid, tcon, &cfile->fid); 724 } else 725 spin_unlock(&cfile->file_info_lock); 726 if (cfile->srch_inf.ntwrk_buf_start) { 727 cifs_dbg(FYI, "freeing SMB ff cache buf on search rewind\n"); 728 if (cfile->srch_inf.smallBuf) 729 cifs_small_buf_release(cfile->srch_inf. 730 ntwrk_buf_start); 731 else 732 cifs_buf_release(cfile->srch_inf. 733 ntwrk_buf_start); 734 cfile->srch_inf.ntwrk_buf_start = NULL; 735 } 736 rc = initiate_cifs_search(xid, file, full_path); 737 if (rc) { 738 cifs_dbg(FYI, "error %d reinitiating a search on rewind\n", 739 rc); 740 return rc; 741 } 742 /* FindFirst/Next set last_entry to NULL on malformed reply */ 743 if (cfile->srch_inf.last_entry) 744 cifs_save_resume_key(cfile->srch_inf.last_entry, cfile); 745 } 746 747 search_flags = CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME; 748 if (backup_cred(cifs_sb)) 749 search_flags |= CIFS_SEARCH_BACKUP_SEARCH; 750 751 while ((index_to_find >= cfile->srch_inf.index_of_last_entry) && 752 (rc == 0) && !cfile->srch_inf.endOfSearch) { 753 cifs_dbg(FYI, "calling findnext2\n"); 754 rc = server->ops->query_dir_next(xid, tcon, &cfile->fid, 755 search_flags, 756 &cfile->srch_inf); 757 /* FindFirst/Next set last_entry to NULL on malformed reply */ 758 if (cfile->srch_inf.last_entry) 759 cifs_save_resume_key(cfile->srch_inf.last_entry, cfile); 760 if (rc) 761 return -ENOENT; 762 } 763 if (index_to_find < cfile->srch_inf.index_of_last_entry) { 764 /* we found the buffer that contains the entry */ 765 /* scan and find it */ 766 int i; 767 char *cur_ent; 768 char *end_of_smb; 769 770 if (cfile->srch_inf.ntwrk_buf_start == NULL) { 771 cifs_dbg(VFS, "ntwrk_buf_start is NULL during readdir\n"); 772 return -EIO; 773 } 774 775 end_of_smb = cfile->srch_inf.ntwrk_buf_start + 776 server->ops->calc_smb_size( 777 cfile->srch_inf.ntwrk_buf_start); 778 779 cur_ent = cfile->srch_inf.srch_entries_start; 780 first_entry_in_buffer = cfile->srch_inf.index_of_last_entry 781 - cfile->srch_inf.entries_in_buffer; 782 pos_in_buf = index_to_find - first_entry_in_buffer; 783 cifs_dbg(FYI, "found entry - pos_in_buf %d\n", pos_in_buf); 784 785 for (i = 0; (i < (pos_in_buf)) && (cur_ent != NULL); i++) { 786 /* go entry by entry figuring out which is first */ 787 cur_ent = nxt_dir_entry(cur_ent, end_of_smb, 788 cfile->srch_inf.info_level); 789 } 790 if ((cur_ent == NULL) && (i < pos_in_buf)) { 791 /* BB fixme - check if we should flag this error */ 792 cifs_dbg(VFS, "reached end of buf searching for pos in buf %d index to find %lld rc %d\n", 793 pos_in_buf, index_to_find, rc); 794 } 795 rc = 0; 796 *current_entry = cur_ent; 797 } else { 798 cifs_dbg(FYI, "index not in buffer - could not findnext into it\n"); 799 return 0; 800 } 801 802 if (pos_in_buf >= cfile->srch_inf.entries_in_buffer) { 803 cifs_dbg(FYI, "can not return entries pos_in_buf beyond last\n"); 804 *num_to_ret = 0; 805 } else 806 *num_to_ret = cfile->srch_inf.entries_in_buffer - pos_in_buf; 807 808 return rc; 809 } 810 811 static bool emit_cached_dirents(struct cached_dirents *cde, 812 struct dir_context *ctx) 813 { 814 struct cached_dirent *dirent; 815 bool rc; 816 817 list_for_each_entry(dirent, &cde->entries, entry) { 818 /* 819 * Skip all early entries prior to the current lseek() 820 * position. 821 */ 822 if (ctx->pos > dirent->pos) 823 continue; 824 /* 825 * We recorded the current ->pos value for the dirent 826 * when we stored it in the cache. 827 * However, this sequence of ->pos values may have holes 828 * in it, for example dot-dirs returned from the server 829 * are suppressed. 830 * Handle this bu forcing ctx->pos to be the same as the 831 * ->pos of the current dirent we emit from the cache. 832 * This means that when we emit these entries from the cache 833 * we now emit them with the same ->pos value as in the 834 * initial scan. 835 */ 836 ctx->pos = dirent->pos; 837 rc = dir_emit(ctx, dirent->name, dirent->namelen, 838 dirent->fattr.cf_uniqueid, 839 dirent->fattr.cf_dtype); 840 if (!rc) 841 return rc; 842 ctx->pos++; 843 } 844 return true; 845 } 846 847 static void update_cached_dirents_count(struct cached_dirents *cde, 848 struct dir_context *ctx) 849 { 850 if (cde->ctx != ctx) 851 return; 852 if (cde->is_valid || cde->is_failed) 853 return; 854 855 cde->pos++; 856 } 857 858 static void finished_cached_dirents_count(struct cached_dirents *cde, 859 struct dir_context *ctx) 860 { 861 if (cde->ctx != ctx) 862 return; 863 if (cde->is_valid || cde->is_failed) 864 return; 865 if (ctx->pos != cde->pos) 866 return; 867 868 cde->is_valid = 1; 869 } 870 871 static void add_cached_dirent(struct cached_dirents *cde, 872 struct dir_context *ctx, 873 const char *name, int namelen, 874 struct cifs_fattr *fattr) 875 { 876 struct cached_dirent *de; 877 878 if (cde->ctx != ctx) 879 return; 880 if (cde->is_valid || cde->is_failed) 881 return; 882 if (ctx->pos != cde->pos) { 883 cde->is_failed = 1; 884 return; 885 } 886 de = kzalloc(sizeof(*de), GFP_ATOMIC); 887 if (de == NULL) { 888 cde->is_failed = 1; 889 return; 890 } 891 de->namelen = namelen; 892 de->name = kstrndup(name, namelen, GFP_ATOMIC); 893 if (de->name == NULL) { 894 kfree(de); 895 cde->is_failed = 1; 896 return; 897 } 898 de->pos = ctx->pos; 899 900 memcpy(&de->fattr, fattr, sizeof(struct cifs_fattr)); 901 902 list_add_tail(&de->entry, &cde->entries); 903 } 904 905 static bool cifs_dir_emit(struct dir_context *ctx, 906 const char *name, int namelen, 907 struct cifs_fattr *fattr, 908 struct cached_fid *cfid) 909 { 910 bool rc; 911 ino_t ino = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid); 912 913 rc = dir_emit(ctx, name, namelen, ino, fattr->cf_dtype); 914 if (!rc) 915 return rc; 916 917 if (cfid) { 918 mutex_lock(&cfid->dirents.de_mutex); 919 add_cached_dirent(&cfid->dirents, ctx, name, namelen, 920 fattr); 921 mutex_unlock(&cfid->dirents.de_mutex); 922 } 923 924 return rc; 925 } 926 927 static int cifs_filldir(char *find_entry, struct file *file, 928 struct dir_context *ctx, 929 char *scratch_buf, unsigned int max_len, 930 struct cached_fid *cfid) 931 { 932 struct cifsFileInfo *file_info = file->private_data; 933 struct super_block *sb = file_inode(file)->i_sb; 934 struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 935 struct cifs_dirent de = { NULL, }; 936 struct cifs_fattr fattr; 937 struct qstr name; 938 int rc = 0; 939 940 rc = cifs_fill_dirent(&de, find_entry, file_info->srch_inf.info_level, 941 file_info->srch_inf.unicode); 942 if (rc) 943 return rc; 944 945 if (de.namelen > max_len) { 946 cifs_dbg(VFS, "bad search response length %zd past smb end\n", 947 de.namelen); 948 return -EINVAL; 949 } 950 951 /* skip . and .. since we added them first */ 952 if (cifs_entry_is_dot(&de, file_info->srch_inf.unicode)) 953 return 0; 954 955 if (file_info->srch_inf.unicode) { 956 struct nls_table *nlt = cifs_sb->local_nls; 957 int map_type; 958 959 map_type = cifs_remap(cifs_sb); 960 name.name = scratch_buf; 961 name.len = 962 cifs_from_utf16((char *)name.name, (__le16 *)de.name, 963 UNICODE_NAME_MAX, 964 min_t(size_t, de.namelen, 965 (size_t)max_len), nlt, map_type); 966 name.len -= nls_nullsize(nlt); 967 } else { 968 name.name = de.name; 969 name.len = de.namelen; 970 } 971 972 switch (file_info->srch_inf.info_level) { 973 case SMB_FIND_FILE_POSIX_INFO: 974 cifs_posix_to_fattr(&fattr, 975 (struct smb2_posix_info *)find_entry, 976 cifs_sb); 977 break; 978 case SMB_FIND_FILE_UNIX: 979 cifs_unix_basic_to_fattr(&fattr, 980 &((FILE_UNIX_INFO *)find_entry)->basic, 981 cifs_sb); 982 if (S_ISLNK(fattr.cf_mode)) 983 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL; 984 break; 985 case SMB_FIND_FILE_INFO_STANDARD: 986 cifs_std_info_to_fattr(&fattr, 987 (FIND_FILE_STANDARD_INFO *)find_entry, 988 cifs_sb); 989 break; 990 case SMB_FIND_FILE_ID_FULL_DIR_INFO: 991 cifs_fulldir_info_to_fattr(&fattr, 992 (SEARCH_ID_FULL_DIR_INFO *)find_entry, 993 cifs_sb); 994 break; 995 default: 996 cifs_dir_info_to_fattr(&fattr, 997 (FILE_DIRECTORY_INFO *)find_entry, 998 cifs_sb); 999 break; 1000 } 1001 1002 if (de.ino && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) { 1003 fattr.cf_uniqueid = de.ino; 1004 } else { 1005 fattr.cf_uniqueid = iunique(sb, ROOT_I); 1006 cifs_autodisable_serverino(cifs_sb); 1007 } 1008 1009 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) && 1010 couldbe_mf_symlink(&fattr)) 1011 /* 1012 * trying to get the type and mode can be slow, 1013 * so just call those regular files for now, and mark 1014 * for reval 1015 */ 1016 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL; 1017 1018 cifs_prime_dcache(file_dentry(file), &name, &fattr); 1019 1020 return !cifs_dir_emit(ctx, name.name, name.len, 1021 &fattr, cfid); 1022 } 1023 1024 1025 int cifs_readdir(struct file *file, struct dir_context *ctx) 1026 { 1027 int rc = 0; 1028 unsigned int xid; 1029 int i; 1030 struct tcon_link *tlink = NULL; 1031 struct cifs_tcon *tcon; 1032 struct cifsFileInfo *cifsFile; 1033 char *current_entry; 1034 int num_to_fill = 0; 1035 char *tmp_buf = NULL; 1036 char *end_of_smb; 1037 unsigned int max_len; 1038 const char *full_path; 1039 void *page = alloc_dentry_path(); 1040 struct cached_fid *cfid = NULL; 1041 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file); 1042 1043 xid = get_xid(); 1044 1045 full_path = build_path_from_dentry(file_dentry(file), page); 1046 if (IS_ERR(full_path)) { 1047 rc = PTR_ERR(full_path); 1048 goto rddir2_exit; 1049 } 1050 1051 if (file->private_data == NULL) { 1052 tlink = cifs_sb_tlink(cifs_sb); 1053 if (IS_ERR(tlink)) 1054 goto cache_not_found; 1055 tcon = tlink_tcon(tlink); 1056 } else { 1057 cifsFile = file->private_data; 1058 tcon = tlink_tcon(cifsFile->tlink); 1059 } 1060 1061 rc = open_cached_dir(xid, tcon, full_path, cifs_sb, false, &cfid); 1062 cifs_put_tlink(tlink); 1063 if (rc) 1064 goto cache_not_found; 1065 1066 mutex_lock(&cfid->dirents.de_mutex); 1067 /* 1068 * If this was reading from the start of the directory 1069 * we need to initialize scanning and storing the 1070 * directory content. 1071 */ 1072 if (ctx->pos == 0 && cfid->dirents.ctx == NULL) { 1073 cfid->dirents.ctx = ctx; 1074 cfid->dirents.pos = 2; 1075 } 1076 /* 1077 * If we already have the entire directory cached then 1078 * we can just serve the cache. 1079 */ 1080 if (cfid->dirents.is_valid) { 1081 if (!dir_emit_dots(file, ctx)) { 1082 mutex_unlock(&cfid->dirents.de_mutex); 1083 goto rddir2_exit; 1084 } 1085 emit_cached_dirents(&cfid->dirents, ctx); 1086 mutex_unlock(&cfid->dirents.de_mutex); 1087 goto rddir2_exit; 1088 } 1089 mutex_unlock(&cfid->dirents.de_mutex); 1090 1091 /* Drop the cache while calling initiate_cifs_search and 1092 * find_cifs_entry in case there will be reconnects during 1093 * query_directory. 1094 */ 1095 close_cached_dir(cfid); 1096 cfid = NULL; 1097 1098 cache_not_found: 1099 /* 1100 * Ensure FindFirst doesn't fail before doing filldir() for '.' and 1101 * '..'. Otherwise we won't be able to notify VFS in case of failure. 1102 */ 1103 if (file->private_data == NULL) { 1104 rc = initiate_cifs_search(xid, file, full_path); 1105 cifs_dbg(FYI, "initiate cifs search rc %d\n", rc); 1106 if (rc) 1107 goto rddir2_exit; 1108 } 1109 1110 if (!dir_emit_dots(file, ctx)) 1111 goto rddir2_exit; 1112 1113 /* 1) If search is active, 1114 is in current search buffer? 1115 if it before then restart search 1116 if after then keep searching till find it */ 1117 cifsFile = file->private_data; 1118 if (cifsFile->srch_inf.endOfSearch) { 1119 if (cifsFile->srch_inf.emptyDir) { 1120 cifs_dbg(FYI, "End of search, empty dir\n"); 1121 rc = 0; 1122 goto rddir2_exit; 1123 } 1124 } /* else { 1125 cifsFile->invalidHandle = true; 1126 tcon->ses->server->close(xid, tcon, &cifsFile->fid); 1127 } */ 1128 1129 tcon = tlink_tcon(cifsFile->tlink); 1130 rc = find_cifs_entry(xid, tcon, ctx->pos, file, full_path, 1131 ¤t_entry, &num_to_fill); 1132 open_cached_dir(xid, tcon, full_path, cifs_sb, false, &cfid); 1133 if (rc) { 1134 cifs_dbg(FYI, "fce error %d\n", rc); 1135 goto rddir2_exit; 1136 } else if (current_entry != NULL) { 1137 cifs_dbg(FYI, "entry %lld found\n", ctx->pos); 1138 } else { 1139 if (cfid) { 1140 mutex_lock(&cfid->dirents.de_mutex); 1141 finished_cached_dirents_count(&cfid->dirents, ctx); 1142 mutex_unlock(&cfid->dirents.de_mutex); 1143 } 1144 cifs_dbg(FYI, "Could not find entry\n"); 1145 goto rddir2_exit; 1146 } 1147 cifs_dbg(FYI, "loop through %d times filling dir for net buf %p\n", 1148 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start); 1149 max_len = tcon->ses->server->ops->calc_smb_size( 1150 cifsFile->srch_inf.ntwrk_buf_start); 1151 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len; 1152 1153 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL); 1154 if (tmp_buf == NULL) { 1155 rc = -ENOMEM; 1156 goto rddir2_exit; 1157 } 1158 1159 for (i = 0; i < num_to_fill; i++) { 1160 if (current_entry == NULL) { 1161 /* evaluate whether this case is an error */ 1162 cifs_dbg(VFS, "past SMB end, num to fill %d i %d\n", 1163 num_to_fill, i); 1164 break; 1165 } 1166 /* 1167 * if buggy server returns . and .. late do we want to 1168 * check for that here? 1169 */ 1170 *tmp_buf = 0; 1171 rc = cifs_filldir(current_entry, file, ctx, 1172 tmp_buf, max_len, cfid); 1173 if (rc) { 1174 if (rc > 0) 1175 rc = 0; 1176 break; 1177 } 1178 1179 ctx->pos++; 1180 if (cfid) { 1181 mutex_lock(&cfid->dirents.de_mutex); 1182 update_cached_dirents_count(&cfid->dirents, ctx); 1183 mutex_unlock(&cfid->dirents.de_mutex); 1184 } 1185 1186 if (ctx->pos == 1187 cifsFile->srch_inf.index_of_last_entry) { 1188 cifs_dbg(FYI, "last entry in buf at pos %lld %s\n", 1189 ctx->pos, tmp_buf); 1190 cifs_save_resume_key(current_entry, cifsFile); 1191 break; 1192 } 1193 current_entry = 1194 nxt_dir_entry(current_entry, end_of_smb, 1195 cifsFile->srch_inf.info_level); 1196 } 1197 kfree(tmp_buf); 1198 1199 rddir2_exit: 1200 if (cfid) 1201 close_cached_dir(cfid); 1202 free_dentry_path(page); 1203 free_xid(xid); 1204 return rc; 1205 } 1206