readdir.c 24 KB

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