readdir.c 34 KB

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