readdir.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*
  2. * fs/cifs/readdir.c
  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. * This library is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published
  12. * by the Free Software Foundation; either version 2.1 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  18. * the GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/fs.h>
  25. #include <linux/pagemap.h>
  26. #include <linux/slab.h>
  27. #include <linux/stat.h>
  28. #include "cifspdu.h"
  29. #include "cifsglob.h"
  30. #include "cifsproto.h"
  31. #include "cifs_unicode.h"
  32. #include "cifs_debug.h"
  33. #include "cifs_fs_sb.h"
  34. #include "cifsfs.h"
  35. /*
  36. * To be safe - for UCS to UTF-8 with strings loaded with the rare long
  37. * characters alloc more to account for such multibyte target UTF-8
  38. * characters.
  39. */
  40. #define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2)
  41. #ifdef CONFIG_CIFS_DEBUG2
  42. static void dump_cifs_file_struct(struct file *file, char *label)
  43. {
  44. struct cifsFileInfo *cf;
  45. if (file) {
  46. cf = file->private_data;
  47. if (cf == NULL) {
  48. cFYI(1, "empty cifs private file data");
  49. return;
  50. }
  51. if (cf->invalidHandle)
  52. cFYI(1, "invalid handle");
  53. if (cf->srch_inf.endOfSearch)
  54. cFYI(1, "end of search");
  55. if (cf->srch_inf.emptyDir)
  56. cFYI(1, "empty dir");
  57. }
  58. }
  59. #else
  60. static inline void dump_cifs_file_struct(struct file *file, char *label)
  61. {
  62. }
  63. #endif /* DEBUG2 */
  64. /*
  65. * Find the dentry that matches "name". If there isn't one, create one. If it's
  66. * a negative dentry or the uniqueid changed, then drop it and recreate it.
  67. */
  68. static struct dentry *
  69. cifs_readdir_lookup(struct dentry *parent, struct qstr *name,
  70. struct cifs_fattr *fattr)
  71. {
  72. struct dentry *dentry, *alias;
  73. struct inode *inode;
  74. struct super_block *sb = parent->d_inode->i_sb;
  75. cFYI(1, "For %s", name->name);
  76. if (parent->d_op && parent->d_op->d_hash)
  77. parent->d_op->d_hash(parent, parent->d_inode, name);
  78. else
  79. name->hash = full_name_hash(name->name, name->len);
  80. dentry = d_lookup(parent, name);
  81. if (dentry) {
  82. /* FIXME: check for inode number changes? */
  83. if (dentry->d_inode != NULL)
  84. return dentry;
  85. d_drop(dentry);
  86. dput(dentry);
  87. }
  88. dentry = d_alloc(parent, name);
  89. if (dentry == NULL)
  90. return NULL;
  91. inode = cifs_iget(sb, fattr);
  92. if (!inode) {
  93. dput(dentry);
  94. return NULL;
  95. }
  96. alias = d_materialise_unique(dentry, inode);
  97. if (alias != NULL) {
  98. dput(dentry);
  99. if (IS_ERR(alias))
  100. return NULL;
  101. dentry = alias;
  102. }
  103. return dentry;
  104. }
  105. static void
  106. cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb)
  107. {
  108. fattr->cf_uid = cifs_sb->mnt_uid;
  109. fattr->cf_gid = cifs_sb->mnt_gid;
  110. if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
  111. fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
  112. fattr->cf_dtype = DT_DIR;
  113. } else {
  114. fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
  115. fattr->cf_dtype = DT_REG;
  116. }
  117. if (fattr->cf_cifsattrs & ATTR_READONLY)
  118. fattr->cf_mode &= ~S_IWUGO;
  119. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL &&
  120. fattr->cf_cifsattrs & ATTR_SYSTEM) {
  121. if (fattr->cf_eof == 0) {
  122. fattr->cf_mode &= ~S_IFMT;
  123. fattr->cf_mode |= S_IFIFO;
  124. fattr->cf_dtype = DT_FIFO;
  125. } else {
  126. /*
  127. * trying to get the type and mode via SFU can be slow,
  128. * so just call those regular files for now, and mark
  129. * for reval
  130. */
  131. fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
  132. }
  133. }
  134. }
  135. static void
  136. cifs_dir_info_to_fattr(struct cifs_fattr *fattr, FILE_DIRECTORY_INFO *info,
  137. struct cifs_sb_info *cifs_sb)
  138. {
  139. memset(fattr, 0, sizeof(*fattr));
  140. fattr->cf_cifsattrs = le32_to_cpu(info->ExtFileAttributes);
  141. fattr->cf_eof = le64_to_cpu(info->EndOfFile);
  142. fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
  143. fattr->cf_createtime = le64_to_cpu(info->CreationTime);
  144. fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
  145. fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
  146. fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
  147. cifs_fill_common_info(fattr, cifs_sb);
  148. }
  149. static void
  150. cifs_std_info_to_fattr(struct cifs_fattr *fattr, FIND_FILE_STANDARD_INFO *info,
  151. struct cifs_sb_info *cifs_sb)
  152. {
  153. int offset = cifs_sb_master_tcon(cifs_sb)->ses->server->timeAdj;
  154. memset(fattr, 0, sizeof(*fattr));
  155. fattr->cf_atime = cnvrtDosUnixTm(info->LastAccessDate,
  156. info->LastAccessTime, offset);
  157. fattr->cf_ctime = cnvrtDosUnixTm(info->LastWriteDate,
  158. info->LastWriteTime, offset);
  159. fattr->cf_mtime = cnvrtDosUnixTm(info->LastWriteDate,
  160. info->LastWriteTime, offset);
  161. fattr->cf_cifsattrs = le16_to_cpu(info->Attributes);
  162. fattr->cf_bytes = le32_to_cpu(info->AllocationSize);
  163. fattr->cf_eof = le32_to_cpu(info->DataSize);
  164. cifs_fill_common_info(fattr, cifs_sb);
  165. }
  166. /* BB eventually need to add the following helper function to
  167. resolve NT_STATUS_STOPPED_ON_SYMLINK return code when
  168. we try to do FindFirst on (NTFS) directory symlinks */
  169. /*
  170. int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb,
  171. int xid)
  172. {
  173. __u16 fid;
  174. int len;
  175. int oplock = 0;
  176. int rc;
  177. struct cifs_tcon *ptcon = cifs_sb_tcon(cifs_sb);
  178. char *tmpbuffer;
  179. rc = CIFSSMBOpen(xid, ptcon, full_path, FILE_OPEN, GENERIC_READ,
  180. OPEN_REPARSE_POINT, &fid, &oplock, NULL,
  181. cifs_sb->local_nls,
  182. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  183. if (!rc) {
  184. tmpbuffer = kmalloc(maxpath);
  185. rc = CIFSSMBQueryReparseLinkInfo(xid, ptcon, full_path,
  186. tmpbuffer,
  187. maxpath -1,
  188. fid,
  189. cifs_sb->local_nls);
  190. if (CIFSSMBClose(xid, ptcon, fid)) {
  191. cFYI(1, "Error closing temporary reparsepoint open");
  192. }
  193. }
  194. }
  195. */
  196. static int initiate_cifs_search(const int xid, struct file *file)
  197. {
  198. __u16 search_flags;
  199. int rc = 0;
  200. char *full_path = NULL;
  201. struct cifsFileInfo *cifsFile;
  202. struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  203. struct tcon_link *tlink = NULL;
  204. struct cifs_tcon *pTcon;
  205. if (file->private_data == NULL) {
  206. tlink = cifs_sb_tlink(cifs_sb);
  207. if (IS_ERR(tlink))
  208. return PTR_ERR(tlink);
  209. cifsFile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
  210. if (cifsFile == NULL) {
  211. rc = -ENOMEM;
  212. goto error_exit;
  213. }
  214. file->private_data = cifsFile;
  215. cifsFile->tlink = cifs_get_tlink(tlink);
  216. pTcon = tlink_tcon(tlink);
  217. } else {
  218. cifsFile = file->private_data;
  219. pTcon = tlink_tcon(cifsFile->tlink);
  220. }
  221. cifsFile->invalidHandle = true;
  222. cifsFile->srch_inf.endOfSearch = false;
  223. full_path = build_path_from_dentry(file->f_path.dentry);
  224. if (full_path == NULL) {
  225. rc = -ENOMEM;
  226. goto error_exit;
  227. }
  228. cFYI(1, "Full path: %s start at: %lld", full_path, file->f_pos);
  229. ffirst_retry:
  230. /* test for Unix extensions */
  231. /* but now check for them on the share/mount not on the SMB session */
  232. /* if (pTcon->ses->capabilities & CAP_UNIX) { */
  233. if (pTcon->unix_ext)
  234. cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
  235. else if ((pTcon->ses->capabilities &
  236. (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
  237. cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
  238. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  239. cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
  240. } else /* not srvinos - BB fixme add check for backlevel? */ {
  241. cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
  242. }
  243. search_flags = CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME;
  244. if (backup_cred(cifs_sb))
  245. search_flags |= CIFS_SEARCH_BACKUP_SEARCH;
  246. rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
  247. &cifsFile->netfid, search_flags, &cifsFile->srch_inf,
  248. cifs_sb->mnt_cifs_flags &
  249. CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
  250. if (rc == 0)
  251. cifsFile->invalidHandle = false;
  252. /* BB add following call to handle readdir on new NTFS symlink errors
  253. else if STATUS_STOPPED_ON_SYMLINK
  254. call get_symlink_reparse_path and retry with new path */
  255. else if ((rc == -EOPNOTSUPP) &&
  256. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
  257. cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
  258. goto ffirst_retry;
  259. }
  260. error_exit:
  261. kfree(full_path);
  262. cifs_put_tlink(tlink);
  263. return rc;
  264. }
  265. /* return length of unicode string in bytes */
  266. static int cifs_unicode_bytelen(const char *str)
  267. {
  268. int len;
  269. const __le16 *ustr = (const __le16 *)str;
  270. for (len = 0; len <= PATH_MAX; len++) {
  271. if (ustr[len] == 0)
  272. return len << 1;
  273. }
  274. cFYI(1, "Unicode string longer than PATH_MAX found");
  275. return len << 1;
  276. }
  277. static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
  278. {
  279. char *new_entry;
  280. FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
  281. if (level == SMB_FIND_FILE_INFO_STANDARD) {
  282. FIND_FILE_STANDARD_INFO *pfData;
  283. pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
  284. new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
  285. pfData->FileNameLength;
  286. } else
  287. new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
  288. cFYI(1, "new entry %p old entry %p", new_entry, old_entry);
  289. /* validate that new_entry is not past end of SMB */
  290. if (new_entry >= end_of_smb) {
  291. cERROR(1, "search entry %p began after end of SMB %p old entry %p",
  292. new_entry, end_of_smb, old_entry);
  293. return NULL;
  294. } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
  295. (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
  296. || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
  297. (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
  298. cERROR(1, "search entry %p extends after end of SMB %p",
  299. new_entry, end_of_smb);
  300. return NULL;
  301. } else
  302. return new_entry;
  303. }
  304. struct cifs_dirent {
  305. const char *name;
  306. size_t namelen;
  307. u32 resume_key;
  308. u64 ino;
  309. };
  310. static void cifs_fill_dirent_unix(struct cifs_dirent *de,
  311. const FILE_UNIX_INFO *info, bool is_unicode)
  312. {
  313. de->name = &info->FileName[0];
  314. if (is_unicode)
  315. de->namelen = cifs_unicode_bytelen(de->name);
  316. else
  317. de->namelen = strnlen(de->name, PATH_MAX);
  318. de->resume_key = info->ResumeKey;
  319. de->ino = le64_to_cpu(info->basic.UniqueId);
  320. }
  321. static void cifs_fill_dirent_dir(struct cifs_dirent *de,
  322. const FILE_DIRECTORY_INFO *info)
  323. {
  324. de->name = &info->FileName[0];
  325. de->namelen = le32_to_cpu(info->FileNameLength);
  326. de->resume_key = info->FileIndex;
  327. }
  328. static void cifs_fill_dirent_full(struct cifs_dirent *de,
  329. const FILE_FULL_DIRECTORY_INFO *info)
  330. {
  331. de->name = &info->FileName[0];
  332. de->namelen = le32_to_cpu(info->FileNameLength);
  333. de->resume_key = info->FileIndex;
  334. }
  335. static void cifs_fill_dirent_search(struct cifs_dirent *de,
  336. const SEARCH_ID_FULL_DIR_INFO *info)
  337. {
  338. de->name = &info->FileName[0];
  339. de->namelen = le32_to_cpu(info->FileNameLength);
  340. de->resume_key = info->FileIndex;
  341. de->ino = le64_to_cpu(info->UniqueId);
  342. }
  343. static void cifs_fill_dirent_both(struct cifs_dirent *de,
  344. const FILE_BOTH_DIRECTORY_INFO *info)
  345. {
  346. de->name = &info->FileName[0];
  347. de->namelen = le32_to_cpu(info->FileNameLength);
  348. de->resume_key = info->FileIndex;
  349. }
  350. static void cifs_fill_dirent_std(struct cifs_dirent *de,
  351. const FIND_FILE_STANDARD_INFO *info)
  352. {
  353. de->name = &info->FileName[0];
  354. /* one byte length, no endianess conversion */
  355. de->namelen = info->FileNameLength;
  356. de->resume_key = info->ResumeKey;
  357. }
  358. static int cifs_fill_dirent(struct cifs_dirent *de, const void *info,
  359. u16 level, bool is_unicode)
  360. {
  361. memset(de, 0, sizeof(*de));
  362. switch (level) {
  363. case SMB_FIND_FILE_UNIX:
  364. cifs_fill_dirent_unix(de, info, is_unicode);
  365. break;
  366. case SMB_FIND_FILE_DIRECTORY_INFO:
  367. cifs_fill_dirent_dir(de, info);
  368. break;
  369. case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
  370. cifs_fill_dirent_full(de, info);
  371. break;
  372. case SMB_FIND_FILE_ID_FULL_DIR_INFO:
  373. cifs_fill_dirent_search(de, info);
  374. break;
  375. case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
  376. cifs_fill_dirent_both(de, info);
  377. break;
  378. case SMB_FIND_FILE_INFO_STANDARD:
  379. cifs_fill_dirent_std(de, info);
  380. break;
  381. default:
  382. cFYI(1, "Unknown findfirst level %d", level);
  383. return -EINVAL;
  384. }
  385. return 0;
  386. }
  387. #define UNICODE_DOT cpu_to_le16(0x2e)
  388. /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
  389. static int cifs_entry_is_dot(struct cifs_dirent *de, bool is_unicode)
  390. {
  391. int rc = 0;
  392. if (!de->name)
  393. return 0;
  394. if (is_unicode) {
  395. __le16 *ufilename = (__le16 *)de->name;
  396. if (de->namelen == 2) {
  397. /* check for . */
  398. if (ufilename[0] == UNICODE_DOT)
  399. rc = 1;
  400. } else if (de->namelen == 4) {
  401. /* check for .. */
  402. if (ufilename[0] == UNICODE_DOT &&
  403. ufilename[1] == UNICODE_DOT)
  404. rc = 2;
  405. }
  406. } else /* ASCII */ {
  407. if (de->namelen == 1) {
  408. if (de->name[0] == '.')
  409. rc = 1;
  410. } else if (de->namelen == 2) {
  411. if (de->name[0] == '.' && de->name[1] == '.')
  412. rc = 2;
  413. }
  414. }
  415. return rc;
  416. }
  417. /* Check if directory that we are searching has changed so we can decide
  418. whether we can use the cached search results from the previous search */
  419. static int is_dir_changed(struct file *file)
  420. {
  421. struct inode *inode = file->f_path.dentry->d_inode;
  422. struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
  423. if (cifsInfo->time == 0)
  424. return 1; /* directory was changed, perhaps due to unlink */
  425. else
  426. return 0;
  427. }
  428. static int cifs_save_resume_key(const char *current_entry,
  429. struct cifsFileInfo *file_info)
  430. {
  431. struct cifs_dirent de;
  432. int rc;
  433. rc = cifs_fill_dirent(&de, current_entry, file_info->srch_inf.info_level,
  434. file_info->srch_inf.unicode);
  435. if (!rc) {
  436. file_info->srch_inf.presume_name = de.name;
  437. file_info->srch_inf.resume_name_len = de.namelen;
  438. file_info->srch_inf.resume_key = de.resume_key;
  439. }
  440. return rc;
  441. }
  442. /* find the corresponding entry in the search */
  443. /* Note that the SMB server returns search entries for . and .. which
  444. complicates logic here if we choose to parse for them and we do not
  445. assume that they are located in the findfirst return buffer.*/
  446. /* We start counting in the buffer with entry 2 and increment for every
  447. entry (do not increment for . or .. entry) */
  448. static int find_cifs_entry(const int xid, struct cifs_tcon *pTcon,
  449. struct file *file, char **ppCurrentEntry, int *num_to_ret)
  450. {
  451. __u16 search_flags;
  452. int rc = 0;
  453. int pos_in_buf = 0;
  454. loff_t first_entry_in_buffer;
  455. loff_t index_to_find = file->f_pos;
  456. struct cifsFileInfo *cifsFile = file->private_data;
  457. struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  458. /* check if index in the buffer */
  459. if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
  460. (num_to_ret == NULL))
  461. return -ENOENT;
  462. *ppCurrentEntry = NULL;
  463. first_entry_in_buffer =
  464. cifsFile->srch_inf.index_of_last_entry -
  465. cifsFile->srch_inf.entries_in_buffer;
  466. /* if first entry in buf is zero then is first buffer
  467. in search response data which means it is likely . and ..
  468. will be in this buffer, although some servers do not return
  469. . and .. for the root of a drive and for those we need
  470. to start two entries earlier */
  471. dump_cifs_file_struct(file, "In fce ");
  472. if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
  473. is_dir_changed(file)) ||
  474. (index_to_find < first_entry_in_buffer)) {
  475. /* close and restart search */
  476. cFYI(1, "search backing up - close and restart search");
  477. spin_lock(&cifs_file_list_lock);
  478. if (!cifsFile->srch_inf.endOfSearch &&
  479. !cifsFile->invalidHandle) {
  480. cifsFile->invalidHandle = true;
  481. spin_unlock(&cifs_file_list_lock);
  482. CIFSFindClose(xid, pTcon, cifsFile->netfid);
  483. } else
  484. spin_unlock(&cifs_file_list_lock);
  485. if (cifsFile->srch_inf.ntwrk_buf_start) {
  486. cFYI(1, "freeing SMB ff cache buf on search rewind");
  487. if (cifsFile->srch_inf.smallBuf)
  488. cifs_small_buf_release(cifsFile->srch_inf.
  489. ntwrk_buf_start);
  490. else
  491. cifs_buf_release(cifsFile->srch_inf.
  492. ntwrk_buf_start);
  493. cifsFile->srch_inf.ntwrk_buf_start = NULL;
  494. }
  495. rc = initiate_cifs_search(xid, file);
  496. if (rc) {
  497. cFYI(1, "error %d reinitiating a search on rewind",
  498. rc);
  499. return rc;
  500. }
  501. /* FindFirst/Next set last_entry to NULL on malformed reply */
  502. if (cifsFile->srch_inf.last_entry)
  503. cifs_save_resume_key(cifsFile->srch_inf.last_entry,
  504. cifsFile);
  505. }
  506. search_flags = CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME;
  507. if (backup_cred(cifs_sb))
  508. search_flags |= CIFS_SEARCH_BACKUP_SEARCH;
  509. while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
  510. (rc == 0) && !cifsFile->srch_inf.endOfSearch) {
  511. cFYI(1, "calling findnext2");
  512. rc = CIFSFindNext(xid, pTcon, cifsFile->netfid, search_flags,
  513. &cifsFile->srch_inf);
  514. /* FindFirst/Next set last_entry to NULL on malformed reply */
  515. if (cifsFile->srch_inf.last_entry)
  516. cifs_save_resume_key(cifsFile->srch_inf.last_entry,
  517. cifsFile);
  518. if (rc)
  519. return -ENOENT;
  520. }
  521. if (index_to_find < cifsFile->srch_inf.index_of_last_entry) {
  522. /* we found the buffer that contains the entry */
  523. /* scan and find it */
  524. int i;
  525. char *current_entry;
  526. char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
  527. smbCalcSize((struct smb_hdr *)
  528. cifsFile->srch_inf.ntwrk_buf_start);
  529. current_entry = cifsFile->srch_inf.srch_entries_start;
  530. first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
  531. - cifsFile->srch_inf.entries_in_buffer;
  532. pos_in_buf = index_to_find - first_entry_in_buffer;
  533. cFYI(1, "found entry - pos_in_buf %d", pos_in_buf);
  534. for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
  535. /* go entry by entry figuring out which is first */
  536. current_entry = nxt_dir_entry(current_entry, end_of_smb,
  537. cifsFile->srch_inf.info_level);
  538. }
  539. if ((current_entry == NULL) && (i < pos_in_buf)) {
  540. /* BB fixme - check if we should flag this error */
  541. cERROR(1, "reached end of buf searching for pos in buf"
  542. " %d index to find %lld rc %d",
  543. pos_in_buf, index_to_find, rc);
  544. }
  545. rc = 0;
  546. *ppCurrentEntry = current_entry;
  547. } else {
  548. cFYI(1, "index not in buffer - could not findnext into it");
  549. return 0;
  550. }
  551. if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
  552. cFYI(1, "can not return entries pos_in_buf beyond last");
  553. *num_to_ret = 0;
  554. } else
  555. *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
  556. return rc;
  557. }
  558. static int cifs_filldir(char *find_entry, struct file *file, filldir_t filldir,
  559. void *dirent, char *scratch_buf, unsigned int max_len)
  560. {
  561. struct cifsFileInfo *file_info = file->private_data;
  562. struct super_block *sb = file->f_path.dentry->d_sb;
  563. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  564. struct cifs_dirent de = { NULL, };
  565. struct cifs_fattr fattr;
  566. struct dentry *dentry;
  567. struct qstr name;
  568. int rc = 0;
  569. ino_t ino;
  570. rc = cifs_fill_dirent(&de, find_entry, file_info->srch_inf.info_level,
  571. file_info->srch_inf.unicode);
  572. if (rc)
  573. return rc;
  574. if (de.namelen > max_len) {
  575. cERROR(1, "bad search response length %zd past smb end",
  576. de.namelen);
  577. return -EINVAL;
  578. }
  579. /* skip . and .. since we added them first */
  580. if (cifs_entry_is_dot(&de, file_info->srch_inf.unicode))
  581. return 0;
  582. if (file_info->srch_inf.unicode) {
  583. struct nls_table *nlt = cifs_sb->local_nls;
  584. name.name = scratch_buf;
  585. name.len =
  586. cifs_from_utf16((char *)name.name, (__le16 *)de.name,
  587. UNICODE_NAME_MAX,
  588. min_t(size_t, de.namelen,
  589. (size_t)max_len), nlt,
  590. cifs_sb->mnt_cifs_flags &
  591. CIFS_MOUNT_MAP_SPECIAL_CHR);
  592. name.len -= nls_nullsize(nlt);
  593. } else {
  594. name.name = de.name;
  595. name.len = de.namelen;
  596. }
  597. switch (file_info->srch_inf.info_level) {
  598. case SMB_FIND_FILE_UNIX:
  599. cifs_unix_basic_to_fattr(&fattr,
  600. &((FILE_UNIX_INFO *)find_entry)->basic,
  601. cifs_sb);
  602. break;
  603. case SMB_FIND_FILE_INFO_STANDARD:
  604. cifs_std_info_to_fattr(&fattr,
  605. (FIND_FILE_STANDARD_INFO *)find_entry,
  606. cifs_sb);
  607. break;
  608. default:
  609. cifs_dir_info_to_fattr(&fattr,
  610. (FILE_DIRECTORY_INFO *)find_entry,
  611. cifs_sb);
  612. break;
  613. }
  614. if (de.ino && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
  615. fattr.cf_uniqueid = de.ino;
  616. } else {
  617. fattr.cf_uniqueid = iunique(sb, ROOT_I);
  618. cifs_autodisable_serverino(cifs_sb);
  619. }
  620. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) &&
  621. CIFSCouldBeMFSymlink(&fattr))
  622. /*
  623. * trying to get the type and mode can be slow,
  624. * so just call those regular files for now, and mark
  625. * for reval
  626. */
  627. fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
  628. ino = cifs_uniqueid_to_ino_t(fattr.cf_uniqueid);
  629. dentry = cifs_readdir_lookup(file->f_dentry, &name, &fattr);
  630. rc = filldir(dirent, name.name, name.len, file->f_pos, ino,
  631. fattr.cf_dtype);
  632. dput(dentry);
  633. return rc;
  634. }
  635. int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
  636. {
  637. int rc = 0;
  638. int xid, i;
  639. struct cifs_tcon *pTcon;
  640. struct cifsFileInfo *cifsFile = NULL;
  641. char *current_entry;
  642. int num_to_fill = 0;
  643. char *tmp_buf = NULL;
  644. char *end_of_smb;
  645. unsigned int max_len;
  646. xid = GetXid();
  647. /*
  648. * Ensure FindFirst doesn't fail before doing filldir() for '.' and
  649. * '..'. Otherwise we won't be able to notify VFS in case of failure.
  650. */
  651. if (file->private_data == NULL) {
  652. rc = initiate_cifs_search(xid, file);
  653. cFYI(1, "initiate cifs search rc %d", rc);
  654. if (rc)
  655. goto rddir2_exit;
  656. }
  657. switch ((int) file->f_pos) {
  658. case 0:
  659. if (filldir(direntry, ".", 1, file->f_pos,
  660. file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
  661. cERROR(1, "Filldir for current dir failed");
  662. rc = -ENOMEM;
  663. break;
  664. }
  665. file->f_pos++;
  666. case 1:
  667. if (filldir(direntry, "..", 2, file->f_pos,
  668. parent_ino(file->f_path.dentry), DT_DIR) < 0) {
  669. cERROR(1, "Filldir for parent dir failed");
  670. rc = -ENOMEM;
  671. break;
  672. }
  673. file->f_pos++;
  674. default:
  675. /* 1) If search is active,
  676. is in current search buffer?
  677. if it before then restart search
  678. if after then keep searching till find it */
  679. if (file->private_data == NULL) {
  680. rc = -EINVAL;
  681. FreeXid(xid);
  682. return rc;
  683. }
  684. cifsFile = file->private_data;
  685. if (cifsFile->srch_inf.endOfSearch) {
  686. if (cifsFile->srch_inf.emptyDir) {
  687. cFYI(1, "End of search, empty dir");
  688. rc = 0;
  689. break;
  690. }
  691. } /* else {
  692. cifsFile->invalidHandle = true;
  693. CIFSFindClose(xid, pTcon, cifsFile->netfid);
  694. } */
  695. pTcon = tlink_tcon(cifsFile->tlink);
  696. rc = find_cifs_entry(xid, pTcon, file,
  697. &current_entry, &num_to_fill);
  698. if (rc) {
  699. cFYI(1, "fce error %d", rc);
  700. goto rddir2_exit;
  701. } else if (current_entry != NULL) {
  702. cFYI(1, "entry %lld found", file->f_pos);
  703. } else {
  704. cFYI(1, "could not find entry");
  705. goto rddir2_exit;
  706. }
  707. cFYI(1, "loop through %d times filling dir for net buf %p",
  708. num_to_fill, cifsFile->srch_inf.ntwrk_buf_start);
  709. max_len = smbCalcSize((struct smb_hdr *)
  710. cifsFile->srch_inf.ntwrk_buf_start);
  711. end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
  712. tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
  713. if (tmp_buf == NULL) {
  714. rc = -ENOMEM;
  715. break;
  716. }
  717. for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
  718. if (current_entry == NULL) {
  719. /* evaluate whether this case is an error */
  720. cERROR(1, "past SMB end, num to fill %d i %d",
  721. num_to_fill, i);
  722. break;
  723. }
  724. /* if buggy server returns . and .. late do
  725. we want to check for that here? */
  726. rc = cifs_filldir(current_entry, file,
  727. filldir, direntry, tmp_buf, max_len);
  728. if (rc == -EOVERFLOW) {
  729. rc = 0;
  730. break;
  731. }
  732. file->f_pos++;
  733. if (file->f_pos ==
  734. cifsFile->srch_inf.index_of_last_entry) {
  735. cFYI(1, "last entry in buf at pos %lld %s",
  736. file->f_pos, tmp_buf);
  737. cifs_save_resume_key(current_entry, cifsFile);
  738. break;
  739. } else
  740. current_entry =
  741. nxt_dir_entry(current_entry, end_of_smb,
  742. cifsFile->srch_inf.info_level);
  743. }
  744. kfree(tmp_buf);
  745. break;
  746. } /* end switch */
  747. rddir2_exit:
  748. FreeXid(xid);
  749. return rc;
  750. }