readdir.c 33 KB

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