readdir.c 26 KB

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