readdir.c 24 KB

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