readdir.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. /*
  2. * fs/cifs/readdir.c
  3. *
  4. * Directory search handling
  5. *
  6. * Copyright (C) International Business Machines Corp., 2004, 2008
  7. * Author(s): Steve French (sfrench@us.ibm.com)
  8. *
  9. * This library is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published
  11. * by the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  17. * the GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/fs.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/stat.h>
  26. #include "cifspdu.h"
  27. #include "cifsglob.h"
  28. #include "cifsproto.h"
  29. #include "cifs_unicode.h"
  30. #include "cifs_debug.h"
  31. #include "cifs_fs_sb.h"
  32. #include "cifsfs.h"
  33. #ifdef CONFIG_CIFS_DEBUG2
  34. static void dump_cifs_file_struct(struct file *file, char *label)
  35. {
  36. struct cifsFileInfo *cf;
  37. if (file) {
  38. cf = file->private_data;
  39. if (cf == NULL) {
  40. cFYI(1, ("empty cifs private file data"));
  41. return;
  42. }
  43. if (cf->invalidHandle)
  44. cFYI(1, ("invalid handle"));
  45. if (cf->srch_inf.endOfSearch)
  46. cFYI(1, ("end of search"));
  47. if (cf->srch_inf.emptyDir)
  48. cFYI(1, ("empty dir"));
  49. }
  50. }
  51. #else
  52. static inline void dump_cifs_file_struct(struct file *file, char *label)
  53. {
  54. }
  55. #endif /* DEBUG2 */
  56. /* Returns 1 if new inode created, 2 if both dentry and inode were */
  57. /* Might check in the future if inode number changed so we can rehash inode */
  58. static int
  59. construct_dentry(struct qstr *qstring, struct file *file,
  60. struct inode **ptmp_inode, struct dentry **pnew_dentry,
  61. __u64 *inum)
  62. {
  63. struct dentry *tmp_dentry = NULL;
  64. struct super_block *sb = file->f_path.dentry->d_sb;
  65. int rc = 0;
  66. cFYI(1, ("For %s", qstring->name));
  67. qstring->hash = full_name_hash(qstring->name, qstring->len);
  68. tmp_dentry = d_lookup(file->f_path.dentry, qstring);
  69. if (tmp_dentry) {
  70. /* BB: overwrite old name? i.e. tmp_dentry->d_name and
  71. * tmp_dentry->d_name.len??
  72. */
  73. cFYI(0, ("existing dentry with inode 0x%p",
  74. tmp_dentry->d_inode));
  75. *ptmp_inode = tmp_dentry->d_inode;
  76. if (*ptmp_inode == NULL) {
  77. *ptmp_inode = cifs_new_inode(sb, inum);
  78. if (*ptmp_inode == NULL)
  79. return rc;
  80. rc = 1;
  81. }
  82. } else {
  83. tmp_dentry = d_alloc(file->f_path.dentry, qstring);
  84. if (tmp_dentry == NULL) {
  85. cERROR(1, ("Failed allocating dentry"));
  86. *ptmp_inode = NULL;
  87. return rc;
  88. }
  89. if (CIFS_SB(sb)->tcon->nocase)
  90. tmp_dentry->d_op = &cifs_ci_dentry_ops;
  91. else
  92. tmp_dentry->d_op = &cifs_dentry_ops;
  93. *ptmp_inode = cifs_new_inode(sb, inum);
  94. if (*ptmp_inode == NULL)
  95. return rc;
  96. rc = 2;
  97. }
  98. tmp_dentry->d_time = jiffies;
  99. *pnew_dentry = tmp_dentry;
  100. return rc;
  101. }
  102. static void AdjustForTZ(struct cifsTconInfo *tcon, struct inode *inode)
  103. {
  104. if ((tcon) && (tcon->ses) && (tcon->ses->server)) {
  105. inode->i_ctime.tv_sec += tcon->ses->server->timeAdj;
  106. inode->i_mtime.tv_sec += tcon->ses->server->timeAdj;
  107. inode->i_atime.tv_sec += tcon->ses->server->timeAdj;
  108. }
  109. return;
  110. }
  111. static void fill_in_inode(struct inode *tmp_inode, int new_buf_type,
  112. char *buf, unsigned int *pobject_type, int isNewInode)
  113. {
  114. loff_t local_size;
  115. struct timespec local_mtime;
  116. struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
  117. struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
  118. __u32 attr;
  119. __u64 allocation_size;
  120. __u64 end_of_file;
  121. umode_t default_mode;
  122. /* save mtime and size */
  123. local_mtime = tmp_inode->i_mtime;
  124. local_size = tmp_inode->i_size;
  125. if (new_buf_type) {
  126. FILE_DIRECTORY_INFO *pfindData = (FILE_DIRECTORY_INFO *)buf;
  127. attr = le32_to_cpu(pfindData->ExtFileAttributes);
  128. allocation_size = le64_to_cpu(pfindData->AllocationSize);
  129. end_of_file = le64_to_cpu(pfindData->EndOfFile);
  130. tmp_inode->i_atime =
  131. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
  132. tmp_inode->i_mtime =
  133. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
  134. tmp_inode->i_ctime =
  135. cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
  136. } else { /* legacy, OS2 and DOS style */
  137. /* struct timespec ts;*/
  138. FIND_FILE_STANDARD_INFO *pfindData =
  139. (FIND_FILE_STANDARD_INFO *)buf;
  140. tmp_inode->i_mtime = cnvrtDosUnixTm(
  141. le16_to_cpu(pfindData->LastWriteDate),
  142. le16_to_cpu(pfindData->LastWriteTime));
  143. tmp_inode->i_atime = cnvrtDosUnixTm(
  144. le16_to_cpu(pfindData->LastAccessDate),
  145. le16_to_cpu(pfindData->LastAccessTime));
  146. tmp_inode->i_ctime = cnvrtDosUnixTm(
  147. le16_to_cpu(pfindData->LastWriteDate),
  148. le16_to_cpu(pfindData->LastWriteTime));
  149. AdjustForTZ(cifs_sb->tcon, tmp_inode);
  150. attr = le16_to_cpu(pfindData->Attributes);
  151. allocation_size = le32_to_cpu(pfindData->AllocationSize);
  152. end_of_file = le32_to_cpu(pfindData->DataSize);
  153. }
  154. /* Linux can not store file creation time unfortunately so ignore it */
  155. cifsInfo->cifsAttrs = attr;
  156. #ifdef CONFIG_CIFS_EXPERIMENTAL
  157. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
  158. /* get more accurate mode via ACL - so force inode refresh */
  159. cifsInfo->time = 0;
  160. } else
  161. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  162. cifsInfo->time = jiffies;
  163. /* treat dos attribute of read-only as read-only mode bit e.g. 555? */
  164. /* 2767 perms - indicate mandatory locking */
  165. /* BB fill in uid and gid here? with help from winbind?
  166. or retrieve from NTFS stream extended attribute */
  167. if (atomic_read(&cifsInfo->inUse) == 0) {
  168. tmp_inode->i_uid = cifs_sb->mnt_uid;
  169. tmp_inode->i_gid = cifs_sb->mnt_gid;
  170. }
  171. if (attr & ATTR_DIRECTORY)
  172. default_mode = cifs_sb->mnt_dir_mode;
  173. else
  174. default_mode = cifs_sb->mnt_file_mode;
  175. /* set initial permissions */
  176. if ((atomic_read(&cifsInfo->inUse) == 0) ||
  177. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
  178. tmp_inode->i_mode = default_mode;
  179. else {
  180. /* just reenable write bits if !ATTR_READONLY */
  181. if ((tmp_inode->i_mode & S_IWUGO) == 0 &&
  182. (attr & ATTR_READONLY) == 0)
  183. tmp_inode->i_mode |= (S_IWUGO & default_mode);
  184. tmp_inode->i_mode &= ~S_IFMT;
  185. }
  186. /* clear write bits if ATTR_READONLY is set */
  187. if (attr & ATTR_READONLY)
  188. tmp_inode->i_mode &= ~S_IWUGO;
  189. /* set inode type */
  190. if ((attr & ATTR_SYSTEM) &&
  191. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
  192. if (end_of_file == 0) {
  193. tmp_inode->i_mode |= S_IFIFO;
  194. *pobject_type = DT_FIFO;
  195. } else {
  196. /*
  197. * trying to get the type can be slow, so just call
  198. * this a regular file for now, and mark for reval
  199. */
  200. tmp_inode->i_mode |= S_IFREG;
  201. *pobject_type = DT_REG;
  202. cifsInfo->time = 0;
  203. }
  204. } else {
  205. if (attr & ATTR_DIRECTORY) {
  206. tmp_inode->i_mode |= S_IFDIR;
  207. *pobject_type = DT_DIR;
  208. } else {
  209. tmp_inode->i_mode |= S_IFREG;
  210. *pobject_type = DT_REG;
  211. }
  212. }
  213. /* can not fill in nlink here as in qpathinfo version and Unx search */
  214. if (atomic_read(&cifsInfo->inUse) == 0)
  215. atomic_set(&cifsInfo->inUse, 1);
  216. cifsInfo->server_eof = end_of_file;
  217. spin_lock(&tmp_inode->i_lock);
  218. if (is_size_safe_to_change(cifsInfo, end_of_file)) {
  219. /* can not safely change the file size here if the
  220. client is writing to it due to potential races */
  221. i_size_write(tmp_inode, end_of_file);
  222. /* 512 bytes (2**9) is the fake blocksize that must be used */
  223. /* for this calculation, even though the reported blocksize is larger */
  224. tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9;
  225. }
  226. spin_unlock(&tmp_inode->i_lock);
  227. if (allocation_size < end_of_file)
  228. cFYI(1, ("May be sparse file, allocation less than file size"));
  229. cFYI(1, ("File Size %ld and blocks %llu",
  230. (unsigned long)tmp_inode->i_size,
  231. (unsigned long long)tmp_inode->i_blocks));
  232. if (S_ISREG(tmp_inode->i_mode)) {
  233. cFYI(1, ("File inode"));
  234. tmp_inode->i_op = &cifs_file_inode_ops;
  235. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  236. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  237. tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
  238. else
  239. tmp_inode->i_fop = &cifs_file_direct_ops;
  240. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  241. tmp_inode->i_fop = &cifs_file_nobrl_ops;
  242. else
  243. tmp_inode->i_fop = &cifs_file_ops;
  244. if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
  245. (cifs_sb->tcon->ses->server->maxBuf <
  246. PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
  247. tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
  248. else
  249. tmp_inode->i_data.a_ops = &cifs_addr_ops;
  250. if (isNewInode)
  251. return; /* No sense invalidating pages for new inode
  252. since have not started caching readahead file
  253. data yet */
  254. if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
  255. (local_size == tmp_inode->i_size)) {
  256. cFYI(1, ("inode exists but unchanged"));
  257. } else {
  258. /* file may have changed on server */
  259. cFYI(1, ("invalidate inode, readdir detected change"));
  260. invalidate_remote_inode(tmp_inode);
  261. }
  262. } else if (S_ISDIR(tmp_inode->i_mode)) {
  263. cFYI(1, ("Directory inode"));
  264. tmp_inode->i_op = &cifs_dir_inode_ops;
  265. tmp_inode->i_fop = &cifs_dir_ops;
  266. } else if (S_ISLNK(tmp_inode->i_mode)) {
  267. cFYI(1, ("Symbolic Link inode"));
  268. tmp_inode->i_op = &cifs_symlink_inode_ops;
  269. } else {
  270. cFYI(1, ("Init special inode"));
  271. init_special_inode(tmp_inode, tmp_inode->i_mode,
  272. tmp_inode->i_rdev);
  273. }
  274. }
  275. static void unix_fill_in_inode(struct inode *tmp_inode,
  276. FILE_UNIX_INFO *pfindData, unsigned int *pobject_type, int isNewInode)
  277. {
  278. loff_t local_size;
  279. struct timespec local_mtime;
  280. struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
  281. struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
  282. __u32 type = le32_to_cpu(pfindData->Type);
  283. __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes);
  284. __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
  285. cifsInfo->time = jiffies;
  286. atomic_inc(&cifsInfo->inUse);
  287. /* save mtime and size */
  288. local_mtime = tmp_inode->i_mtime;
  289. local_size = tmp_inode->i_size;
  290. tmp_inode->i_atime =
  291. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
  292. tmp_inode->i_mtime =
  293. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastModificationTime));
  294. tmp_inode->i_ctime =
  295. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastStatusChange));
  296. tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions);
  297. /* since we set the inode type below we need to mask off type
  298. to avoid strange results if bits above were corrupt */
  299. tmp_inode->i_mode &= ~S_IFMT;
  300. if (type == UNIX_FILE) {
  301. *pobject_type = DT_REG;
  302. tmp_inode->i_mode |= S_IFREG;
  303. } else if (type == UNIX_SYMLINK) {
  304. *pobject_type = DT_LNK;
  305. tmp_inode->i_mode |= S_IFLNK;
  306. } else if (type == UNIX_DIR) {
  307. *pobject_type = DT_DIR;
  308. tmp_inode->i_mode |= S_IFDIR;
  309. } else if (type == UNIX_CHARDEV) {
  310. *pobject_type = DT_CHR;
  311. tmp_inode->i_mode |= S_IFCHR;
  312. tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
  313. le64_to_cpu(pfindData->DevMinor) & MINORMASK);
  314. } else if (type == UNIX_BLOCKDEV) {
  315. *pobject_type = DT_BLK;
  316. tmp_inode->i_mode |= S_IFBLK;
  317. tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
  318. le64_to_cpu(pfindData->DevMinor) & MINORMASK);
  319. } else if (type == UNIX_FIFO) {
  320. *pobject_type = DT_FIFO;
  321. tmp_inode->i_mode |= S_IFIFO;
  322. } else if (type == UNIX_SOCKET) {
  323. *pobject_type = DT_SOCK;
  324. tmp_inode->i_mode |= S_IFSOCK;
  325. } else {
  326. /* safest to just call it a file */
  327. *pobject_type = DT_REG;
  328. tmp_inode->i_mode |= S_IFREG;
  329. cFYI(1, ("unknown inode type %d", type));
  330. }
  331. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
  332. tmp_inode->i_uid = cifs_sb->mnt_uid;
  333. else
  334. tmp_inode->i_uid = le64_to_cpu(pfindData->Uid);
  335. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
  336. tmp_inode->i_gid = cifs_sb->mnt_gid;
  337. else
  338. tmp_inode->i_gid = le64_to_cpu(pfindData->Gid);
  339. tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks);
  340. cifsInfo->server_eof = end_of_file;
  341. spin_lock(&tmp_inode->i_lock);
  342. if (is_size_safe_to_change(cifsInfo, end_of_file)) {
  343. /* can not safely change the file size here if the
  344. client is writing to it due to potential races */
  345. i_size_write(tmp_inode, end_of_file);
  346. /* 512 bytes (2**9) is the fake blocksize that must be used */
  347. /* for this calculation, not the real blocksize */
  348. tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
  349. }
  350. spin_unlock(&tmp_inode->i_lock);
  351. if (S_ISREG(tmp_inode->i_mode)) {
  352. cFYI(1, ("File inode"));
  353. tmp_inode->i_op = &cifs_file_inode_ops;
  354. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  355. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  356. tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
  357. else
  358. tmp_inode->i_fop = &cifs_file_direct_ops;
  359. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  360. tmp_inode->i_fop = &cifs_file_nobrl_ops;
  361. else
  362. tmp_inode->i_fop = &cifs_file_ops;
  363. if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
  364. (cifs_sb->tcon->ses->server->maxBuf <
  365. PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
  366. tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
  367. else
  368. tmp_inode->i_data.a_ops = &cifs_addr_ops;
  369. if (isNewInode)
  370. return; /* No sense invalidating pages for new inode
  371. since we have not started caching readahead
  372. file data for it yet */
  373. if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
  374. (local_size == tmp_inode->i_size)) {
  375. cFYI(1, ("inode exists but unchanged"));
  376. } else {
  377. /* file may have changed on server */
  378. cFYI(1, ("invalidate inode, readdir detected change"));
  379. invalidate_remote_inode(tmp_inode);
  380. }
  381. } else if (S_ISDIR(tmp_inode->i_mode)) {
  382. cFYI(1, ("Directory inode"));
  383. tmp_inode->i_op = &cifs_dir_inode_ops;
  384. tmp_inode->i_fop = &cifs_dir_ops;
  385. } else if (S_ISLNK(tmp_inode->i_mode)) {
  386. cFYI(1, ("Symbolic Link inode"));
  387. tmp_inode->i_op = &cifs_symlink_inode_ops;
  388. /* tmp_inode->i_fop = *//* do not need to set to anything */
  389. } else {
  390. cFYI(1, ("Special inode"));
  391. init_special_inode(tmp_inode, tmp_inode->i_mode,
  392. tmp_inode->i_rdev);
  393. }
  394. }
  395. static int initiate_cifs_search(const int xid, struct file *file)
  396. {
  397. int rc = 0;
  398. char *full_path;
  399. struct cifsFileInfo *cifsFile;
  400. struct cifs_sb_info *cifs_sb;
  401. struct cifsTconInfo *pTcon;
  402. if (file->private_data == NULL) {
  403. file->private_data =
  404. kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
  405. }
  406. if (file->private_data == NULL)
  407. return -ENOMEM;
  408. cifsFile = file->private_data;
  409. cifsFile->invalidHandle = true;
  410. cifsFile->srch_inf.endOfSearch = false;
  411. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  412. if (cifs_sb == NULL)
  413. return -EINVAL;
  414. pTcon = cifs_sb->tcon;
  415. if (pTcon == NULL)
  416. return -EINVAL;
  417. full_path = build_path_from_dentry(file->f_path.dentry);
  418. if (full_path == NULL)
  419. return -ENOMEM;
  420. cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
  421. ffirst_retry:
  422. /* test for Unix extensions */
  423. /* but now check for them on the share/mount not on the SMB session */
  424. /* if (pTcon->ses->capabilities & CAP_UNIX) { */
  425. if (pTcon->unix_ext)
  426. cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
  427. else if ((pTcon->ses->capabilities &
  428. (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
  429. cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
  430. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  431. cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
  432. } else /* not srvinos - BB fixme add check for backlevel? */ {
  433. cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
  434. }
  435. rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
  436. &cifsFile->netfid, &cifsFile->srch_inf,
  437. cifs_sb->mnt_cifs_flags &
  438. CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
  439. if (rc == 0)
  440. cifsFile->invalidHandle = false;
  441. if ((rc == -EOPNOTSUPP) &&
  442. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
  443. cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
  444. goto ffirst_retry;
  445. }
  446. kfree(full_path);
  447. return rc;
  448. }
  449. /* return length of unicode string in bytes */
  450. static int cifs_unicode_bytelen(char *str)
  451. {
  452. int len;
  453. __le16 *ustr = (__le16 *)str;
  454. for (len = 0; len <= PATH_MAX; len++) {
  455. if (ustr[len] == 0)
  456. return len << 1;
  457. }
  458. cFYI(1, ("Unicode string longer than PATH_MAX found"));
  459. return len << 1;
  460. }
  461. static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
  462. {
  463. char *new_entry;
  464. FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
  465. if (level == SMB_FIND_FILE_INFO_STANDARD) {
  466. FIND_FILE_STANDARD_INFO *pfData;
  467. pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
  468. new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
  469. pfData->FileNameLength;
  470. } else
  471. new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
  472. cFYI(1, ("new entry %p old entry %p", new_entry, old_entry));
  473. /* validate that new_entry is not past end of SMB */
  474. if (new_entry >= end_of_smb) {
  475. cERROR(1,
  476. ("search entry %p began after end of SMB %p old entry %p",
  477. new_entry, end_of_smb, old_entry));
  478. return NULL;
  479. } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
  480. (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
  481. || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
  482. (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
  483. cERROR(1, ("search entry %p extends after end of SMB %p",
  484. new_entry, end_of_smb));
  485. return NULL;
  486. } else
  487. return new_entry;
  488. }
  489. #define UNICODE_DOT cpu_to_le16(0x2e)
  490. /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
  491. static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
  492. {
  493. int rc = 0;
  494. char *filename = NULL;
  495. int len = 0;
  496. if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
  497. FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
  498. filename = &pFindData->FileName[0];
  499. if (cfile->srch_inf.unicode) {
  500. len = cifs_unicode_bytelen(filename);
  501. } else {
  502. /* BB should we make this strnlen of PATH_MAX? */
  503. len = strnlen(filename, 5);
  504. }
  505. } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
  506. FILE_DIRECTORY_INFO *pFindData =
  507. (FILE_DIRECTORY_INFO *)current_entry;
  508. filename = &pFindData->FileName[0];
  509. len = le32_to_cpu(pFindData->FileNameLength);
  510. } else if (cfile->srch_inf.info_level ==
  511. SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
  512. FILE_FULL_DIRECTORY_INFO *pFindData =
  513. (FILE_FULL_DIRECTORY_INFO *)current_entry;
  514. filename = &pFindData->FileName[0];
  515. len = le32_to_cpu(pFindData->FileNameLength);
  516. } else if (cfile->srch_inf.info_level ==
  517. SMB_FIND_FILE_ID_FULL_DIR_INFO) {
  518. SEARCH_ID_FULL_DIR_INFO *pFindData =
  519. (SEARCH_ID_FULL_DIR_INFO *)current_entry;
  520. filename = &pFindData->FileName[0];
  521. len = le32_to_cpu(pFindData->FileNameLength);
  522. } else if (cfile->srch_inf.info_level ==
  523. SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
  524. FILE_BOTH_DIRECTORY_INFO *pFindData =
  525. (FILE_BOTH_DIRECTORY_INFO *)current_entry;
  526. filename = &pFindData->FileName[0];
  527. len = le32_to_cpu(pFindData->FileNameLength);
  528. } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
  529. FIND_FILE_STANDARD_INFO *pFindData =
  530. (FIND_FILE_STANDARD_INFO *)current_entry;
  531. filename = &pFindData->FileName[0];
  532. len = pFindData->FileNameLength;
  533. } else {
  534. cFYI(1, ("Unknown findfirst level %d",
  535. cfile->srch_inf.info_level));
  536. }
  537. if (filename) {
  538. if (cfile->srch_inf.unicode) {
  539. __le16 *ufilename = (__le16 *)filename;
  540. if (len == 2) {
  541. /* check for . */
  542. if (ufilename[0] == UNICODE_DOT)
  543. rc = 1;
  544. } else if (len == 4) {
  545. /* check for .. */
  546. if ((ufilename[0] == UNICODE_DOT)
  547. && (ufilename[1] == UNICODE_DOT))
  548. rc = 2;
  549. }
  550. } else /* ASCII */ {
  551. if (len == 1) {
  552. if (filename[0] == '.')
  553. rc = 1;
  554. } else if (len == 2) {
  555. if ((filename[0] == '.') && (filename[1] == '.'))
  556. rc = 2;
  557. }
  558. }
  559. }
  560. return rc;
  561. }
  562. /* Check if directory that we are searching has changed so we can decide
  563. whether we can use the cached search results from the previous search */
  564. static int is_dir_changed(struct file *file)
  565. {
  566. struct inode *inode = file->f_path.dentry->d_inode;
  567. struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
  568. if (cifsInfo->time == 0)
  569. return 1; /* directory was changed, perhaps due to unlink */
  570. else
  571. return 0;
  572. }
  573. static int cifs_save_resume_key(const char *current_entry,
  574. struct cifsFileInfo *cifsFile)
  575. {
  576. int rc = 0;
  577. unsigned int len = 0;
  578. __u16 level;
  579. char *filename;
  580. if ((cifsFile == NULL) || (current_entry == NULL))
  581. return -EINVAL;
  582. level = cifsFile->srch_inf.info_level;
  583. if (level == SMB_FIND_FILE_UNIX) {
  584. FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
  585. filename = &pFindData->FileName[0];
  586. if (cifsFile->srch_inf.unicode) {
  587. len = cifs_unicode_bytelen(filename);
  588. } else {
  589. /* BB should we make this strnlen of PATH_MAX? */
  590. len = strnlen(filename, PATH_MAX);
  591. }
  592. cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
  593. } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
  594. FILE_DIRECTORY_INFO *pFindData =
  595. (FILE_DIRECTORY_INFO *)current_entry;
  596. filename = &pFindData->FileName[0];
  597. len = le32_to_cpu(pFindData->FileNameLength);
  598. cifsFile->srch_inf.resume_key = pFindData->FileIndex;
  599. } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
  600. FILE_FULL_DIRECTORY_INFO *pFindData =
  601. (FILE_FULL_DIRECTORY_INFO *)current_entry;
  602. filename = &pFindData->FileName[0];
  603. len = le32_to_cpu(pFindData->FileNameLength);
  604. cifsFile->srch_inf.resume_key = pFindData->FileIndex;
  605. } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
  606. SEARCH_ID_FULL_DIR_INFO *pFindData =
  607. (SEARCH_ID_FULL_DIR_INFO *)current_entry;
  608. filename = &pFindData->FileName[0];
  609. len = le32_to_cpu(pFindData->FileNameLength);
  610. cifsFile->srch_inf.resume_key = pFindData->FileIndex;
  611. } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
  612. FILE_BOTH_DIRECTORY_INFO *pFindData =
  613. (FILE_BOTH_DIRECTORY_INFO *)current_entry;
  614. filename = &pFindData->FileName[0];
  615. len = le32_to_cpu(pFindData->FileNameLength);
  616. cifsFile->srch_inf.resume_key = pFindData->FileIndex;
  617. } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
  618. FIND_FILE_STANDARD_INFO *pFindData =
  619. (FIND_FILE_STANDARD_INFO *)current_entry;
  620. filename = &pFindData->FileName[0];
  621. /* one byte length, no name conversion */
  622. len = (unsigned int)pFindData->FileNameLength;
  623. cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
  624. } else {
  625. cFYI(1, ("Unknown findfirst level %d", level));
  626. return -EINVAL;
  627. }
  628. cifsFile->srch_inf.resume_name_len = len;
  629. cifsFile->srch_inf.presume_name = filename;
  630. return rc;
  631. }
  632. /* find the corresponding entry in the search */
  633. /* Note that the SMB server returns search entries for . and .. which
  634. complicates logic here if we choose to parse for them and we do not
  635. assume that they are located in the findfirst return buffer.*/
  636. /* We start counting in the buffer with entry 2 and increment for every
  637. entry (do not increment for . or .. entry) */
  638. static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
  639. struct file *file, char **ppCurrentEntry, int *num_to_ret)
  640. {
  641. int rc = 0;
  642. int pos_in_buf = 0;
  643. loff_t first_entry_in_buffer;
  644. loff_t index_to_find = file->f_pos;
  645. struct cifsFileInfo *cifsFile = file->private_data;
  646. /* check if index in the buffer */
  647. if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
  648. (num_to_ret == NULL))
  649. return -ENOENT;
  650. *ppCurrentEntry = NULL;
  651. first_entry_in_buffer =
  652. cifsFile->srch_inf.index_of_last_entry -
  653. cifsFile->srch_inf.entries_in_buffer;
  654. /* if first entry in buf is zero then is first buffer
  655. in search response data which means it is likely . and ..
  656. will be in this buffer, although some servers do not return
  657. . and .. for the root of a drive and for those we need
  658. to start two entries earlier */
  659. dump_cifs_file_struct(file, "In fce ");
  660. if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
  661. is_dir_changed(file)) ||
  662. (index_to_find < first_entry_in_buffer)) {
  663. /* close and restart search */
  664. cFYI(1, ("search backing up - close and restart search"));
  665. write_lock(&GlobalSMBSeslock);
  666. if (!cifsFile->srch_inf.endOfSearch &&
  667. !cifsFile->invalidHandle) {
  668. cifsFile->invalidHandle = true;
  669. write_unlock(&GlobalSMBSeslock);
  670. CIFSFindClose(xid, pTcon, cifsFile->netfid);
  671. } else
  672. write_unlock(&GlobalSMBSeslock);
  673. if (cifsFile->srch_inf.ntwrk_buf_start) {
  674. cFYI(1, ("freeing SMB ff cache buf on search rewind"));
  675. if (cifsFile->srch_inf.smallBuf)
  676. cifs_small_buf_release(cifsFile->srch_inf.
  677. ntwrk_buf_start);
  678. else
  679. cifs_buf_release(cifsFile->srch_inf.
  680. ntwrk_buf_start);
  681. cifsFile->srch_inf.ntwrk_buf_start = NULL;
  682. }
  683. rc = initiate_cifs_search(xid, file);
  684. if (rc) {
  685. cFYI(1, ("error %d reinitiating a search on rewind",
  686. rc));
  687. return rc;
  688. }
  689. cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
  690. }
  691. while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
  692. (rc == 0) && !cifsFile->srch_inf.endOfSearch) {
  693. cFYI(1, ("calling findnext2"));
  694. rc = CIFSFindNext(xid, pTcon, cifsFile->netfid,
  695. &cifsFile->srch_inf);
  696. cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
  697. if (rc)
  698. return -ENOENT;
  699. }
  700. if (index_to_find < cifsFile->srch_inf.index_of_last_entry) {
  701. /* we found the buffer that contains the entry */
  702. /* scan and find it */
  703. int i;
  704. char *current_entry;
  705. char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
  706. smbCalcSize((struct smb_hdr *)
  707. cifsFile->srch_inf.ntwrk_buf_start);
  708. current_entry = cifsFile->srch_inf.srch_entries_start;
  709. first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
  710. - cifsFile->srch_inf.entries_in_buffer;
  711. pos_in_buf = index_to_find - first_entry_in_buffer;
  712. cFYI(1, ("found entry - pos_in_buf %d", pos_in_buf));
  713. for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
  714. /* go entry by entry figuring out which is first */
  715. current_entry = nxt_dir_entry(current_entry, end_of_smb,
  716. cifsFile->srch_inf.info_level);
  717. }
  718. if ((current_entry == NULL) && (i < pos_in_buf)) {
  719. /* BB fixme - check if we should flag this error */
  720. cERROR(1, ("reached end of buf searching for pos in buf"
  721. " %d index to find %lld rc %d",
  722. pos_in_buf, index_to_find, rc));
  723. }
  724. rc = 0;
  725. *ppCurrentEntry = current_entry;
  726. } else {
  727. cFYI(1, ("index not in buffer - could not findnext into it"));
  728. return 0;
  729. }
  730. if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
  731. cFYI(1, ("can not return entries pos_in_buf beyond last"));
  732. *num_to_ret = 0;
  733. } else
  734. *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
  735. return rc;
  736. }
  737. /* inode num, inode type and filename returned */
  738. static int cifs_get_name_from_search_buf(struct qstr *pqst,
  739. char *current_entry, __u16 level, unsigned int unicode,
  740. struct cifs_sb_info *cifs_sb, int max_len, __u64 *pinum)
  741. {
  742. int rc = 0;
  743. unsigned int len = 0;
  744. char *filename;
  745. struct nls_table *nlt = cifs_sb->local_nls;
  746. *pinum = 0;
  747. if (level == SMB_FIND_FILE_UNIX) {
  748. FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
  749. filename = &pFindData->FileName[0];
  750. if (unicode) {
  751. len = cifs_unicode_bytelen(filename);
  752. } else {
  753. /* BB should we make this strnlen of PATH_MAX? */
  754. len = strnlen(filename, PATH_MAX);
  755. }
  756. *pinum = le64_to_cpu(pFindData->UniqueId);
  757. } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
  758. FILE_DIRECTORY_INFO *pFindData =
  759. (FILE_DIRECTORY_INFO *)current_entry;
  760. filename = &pFindData->FileName[0];
  761. len = le32_to_cpu(pFindData->FileNameLength);
  762. } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
  763. FILE_FULL_DIRECTORY_INFO *pFindData =
  764. (FILE_FULL_DIRECTORY_INFO *)current_entry;
  765. filename = &pFindData->FileName[0];
  766. len = le32_to_cpu(pFindData->FileNameLength);
  767. } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
  768. SEARCH_ID_FULL_DIR_INFO *pFindData =
  769. (SEARCH_ID_FULL_DIR_INFO *)current_entry;
  770. filename = &pFindData->FileName[0];
  771. len = le32_to_cpu(pFindData->FileNameLength);
  772. *pinum = le64_to_cpu(pFindData->UniqueId);
  773. } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
  774. FILE_BOTH_DIRECTORY_INFO *pFindData =
  775. (FILE_BOTH_DIRECTORY_INFO *)current_entry;
  776. filename = &pFindData->FileName[0];
  777. len = le32_to_cpu(pFindData->FileNameLength);
  778. } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
  779. FIND_FILE_STANDARD_INFO *pFindData =
  780. (FIND_FILE_STANDARD_INFO *)current_entry;
  781. filename = &pFindData->FileName[0];
  782. /* one byte length, no name conversion */
  783. len = (unsigned int)pFindData->FileNameLength;
  784. } else {
  785. cFYI(1, ("Unknown findfirst level %d", level));
  786. return -EINVAL;
  787. }
  788. if (len > max_len) {
  789. cERROR(1, ("bad search response length %d past smb end", len));
  790. return -EINVAL;
  791. }
  792. if (unicode) {
  793. /* BB fixme - test with long names */
  794. /* Note converted filename can be longer than in unicode */
  795. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
  796. pqst->len = cifs_convertUCSpath((char *)pqst->name,
  797. (__le16 *)filename, len/2, nlt);
  798. else
  799. pqst->len = cifs_strfromUCS_le((char *)pqst->name,
  800. (__le16 *)filename, len/2, nlt);
  801. } else {
  802. pqst->name = filename;
  803. pqst->len = len;
  804. }
  805. pqst->hash = full_name_hash(pqst->name, pqst->len);
  806. /* cFYI(1, ("filldir on %s",pqst->name)); */
  807. return rc;
  808. }
  809. static int cifs_filldir(char *pfindEntry, struct file *file,
  810. filldir_t filldir, void *direntry, char *scratch_buf, int max_len)
  811. {
  812. int rc = 0;
  813. struct qstr qstring;
  814. struct cifsFileInfo *pCifsF;
  815. unsigned int obj_type;
  816. __u64 inum;
  817. struct cifs_sb_info *cifs_sb;
  818. struct inode *tmp_inode;
  819. struct dentry *tmp_dentry;
  820. /* get filename and len into qstring */
  821. /* get dentry */
  822. /* decide whether to create and populate ionde */
  823. if ((direntry == NULL) || (file == NULL))
  824. return -EINVAL;
  825. pCifsF = file->private_data;
  826. if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
  827. return -ENOENT;
  828. rc = cifs_entry_is_dot(pfindEntry, pCifsF);
  829. /* skip . and .. since we added them first */
  830. if (rc != 0)
  831. return 0;
  832. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  833. qstring.name = scratch_buf;
  834. rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
  835. pCifsF->srch_inf.info_level,
  836. pCifsF->srch_inf.unicode, cifs_sb,
  837. max_len,
  838. &inum /* returned */);
  839. if (rc)
  840. return rc;
  841. /* only these two infolevels return valid inode numbers */
  842. if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX ||
  843. pCifsF->srch_inf.info_level == SMB_FIND_FILE_ID_FULL_DIR_INFO)
  844. rc = construct_dentry(&qstring, file, &tmp_inode, &tmp_dentry,
  845. &inum);
  846. else
  847. rc = construct_dentry(&qstring, file, &tmp_inode, &tmp_dentry,
  848. NULL);
  849. if ((tmp_inode == NULL) || (tmp_dentry == NULL))
  850. return -ENOMEM;
  851. /* we pass in rc below, indicating whether it is a new inode,
  852. so we can figure out whether to invalidate the inode cached
  853. data if the file has changed */
  854. if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
  855. unix_fill_in_inode(tmp_inode,
  856. (FILE_UNIX_INFO *)pfindEntry,
  857. &obj_type, rc);
  858. else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
  859. fill_in_inode(tmp_inode, 0 /* old level 1 buffer type */,
  860. pfindEntry, &obj_type, rc);
  861. else
  862. fill_in_inode(tmp_inode, 1 /* NT */, pfindEntry, &obj_type, rc);
  863. if (rc) /* new inode - needs to be tied to dentry */ {
  864. d_instantiate(tmp_dentry, tmp_inode);
  865. if (rc == 2)
  866. d_rehash(tmp_dentry);
  867. }
  868. rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
  869. tmp_inode->i_ino, obj_type);
  870. if (rc) {
  871. cFYI(1, ("filldir rc = %d", rc));
  872. /* we can not return filldir errors to the caller
  873. since they are "normal" when the stat blocksize
  874. is too small - we return remapped error instead */
  875. rc = -EOVERFLOW;
  876. }
  877. dput(tmp_dentry);
  878. return rc;
  879. }
  880. int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
  881. {
  882. int rc = 0;
  883. int xid, i;
  884. struct cifs_sb_info *cifs_sb;
  885. struct cifsTconInfo *pTcon;
  886. struct cifsFileInfo *cifsFile = NULL;
  887. char *current_entry;
  888. int num_to_fill = 0;
  889. char *tmp_buf = NULL;
  890. char *end_of_smb;
  891. int max_len;
  892. xid = GetXid();
  893. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  894. pTcon = cifs_sb->tcon;
  895. if (pTcon == NULL)
  896. return -EINVAL;
  897. switch ((int) file->f_pos) {
  898. case 0:
  899. if (filldir(direntry, ".", 1, file->f_pos,
  900. file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
  901. cERROR(1, ("Filldir for current dir failed"));
  902. rc = -ENOMEM;
  903. break;
  904. }
  905. file->f_pos++;
  906. case 1:
  907. if (filldir(direntry, "..", 2, file->f_pos,
  908. file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
  909. cERROR(1, ("Filldir for parent dir failed"));
  910. rc = -ENOMEM;
  911. break;
  912. }
  913. file->f_pos++;
  914. default:
  915. /* 1) If search is active,
  916. is in current search buffer?
  917. if it before then restart search
  918. if after then keep searching till find it */
  919. if (file->private_data == NULL) {
  920. rc = initiate_cifs_search(xid, file);
  921. cFYI(1, ("initiate cifs search rc %d", rc));
  922. if (rc) {
  923. FreeXid(xid);
  924. return rc;
  925. }
  926. }
  927. if (file->private_data == NULL) {
  928. rc = -EINVAL;
  929. FreeXid(xid);
  930. return rc;
  931. }
  932. cifsFile = file->private_data;
  933. if (cifsFile->srch_inf.endOfSearch) {
  934. if (cifsFile->srch_inf.emptyDir) {
  935. cFYI(1, ("End of search, empty dir"));
  936. rc = 0;
  937. break;
  938. }
  939. } /* else {
  940. cifsFile->invalidHandle = true;
  941. CIFSFindClose(xid, pTcon, cifsFile->netfid);
  942. } */
  943. rc = find_cifs_entry(xid, pTcon, file,
  944. &current_entry, &num_to_fill);
  945. if (rc) {
  946. cFYI(1, ("fce error %d", rc));
  947. goto rddir2_exit;
  948. } else if (current_entry != NULL) {
  949. cFYI(1, ("entry %lld found", file->f_pos));
  950. } else {
  951. cFYI(1, ("could not find entry"));
  952. goto rddir2_exit;
  953. }
  954. cFYI(1, ("loop through %d times filling dir for net buf %p",
  955. num_to_fill, cifsFile->srch_inf.ntwrk_buf_start));
  956. max_len = smbCalcSize((struct smb_hdr *)
  957. cifsFile->srch_inf.ntwrk_buf_start);
  958. end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
  959. /* To be safe - for UCS to UTF-8 with strings loaded
  960. with the rare long characters alloc more to account for
  961. such multibyte target UTF-8 characters. cifs_unicode.c,
  962. which actually does the conversion, has the same limit */
  963. tmp_buf = kmalloc((4 * NAME_MAX) + 2, GFP_KERNEL);
  964. for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
  965. if (current_entry == NULL) {
  966. /* evaluate whether this case is an error */
  967. cERROR(1, ("past SMB end, num to fill %d i %d",
  968. num_to_fill, i));
  969. break;
  970. }
  971. /* if buggy server returns . and .. late do
  972. we want to check for that here? */
  973. rc = cifs_filldir(current_entry, file,
  974. filldir, direntry, tmp_buf, max_len);
  975. if (rc == -EOVERFLOW) {
  976. rc = 0;
  977. break;
  978. }
  979. file->f_pos++;
  980. if (file->f_pos ==
  981. cifsFile->srch_inf.index_of_last_entry) {
  982. cFYI(1, ("last entry in buf at pos %lld %s",
  983. file->f_pos, tmp_buf));
  984. cifs_save_resume_key(current_entry, cifsFile);
  985. break;
  986. } else
  987. current_entry =
  988. nxt_dir_entry(current_entry, end_of_smb,
  989. cifsFile->srch_inf.info_level);
  990. }
  991. kfree(tmp_buf);
  992. break;
  993. } /* end switch */
  994. rddir2_exit:
  995. FreeXid(xid);
  996. return rc;
  997. }