readdir.c 24 KB

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