readdir.c 24 KB

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