readdir.c 23 KB

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