readdir.c 26 KB

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