readdir.c 34 KB

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