readdir.c 33 KB

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