readdir.c 24 KB

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