readdir.c 24 KB

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