readdir.c 34 KB

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