readdir.c 25 KB

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