readdir.c 25 KB

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