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