readdir.c 25 KB

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