readdir.c 26 KB

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