readdir.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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 <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. else if ((tmp_inode->i_mode & S_IWUGO) == 0)
  203. /* the ATTR_READONLY flag may have been changed on */
  204. /* server -- set any w bits allowed by mnt_file_mode */
  205. tmp_inode->i_mode |= (S_IWUGO & cifs_sb->mnt_file_mode);
  206. } /* could add code here - to validate if device or weird share type? */
  207. /* can not fill in nlink here as in qpathinfo version and Unx search */
  208. if (atomic_read(&cifsInfo->inUse) == 0) {
  209. atomic_set(&cifsInfo->inUse, 1);
  210. }
  211. spin_lock(&tmp_inode->i_lock);
  212. if (is_size_safe_to_change(cifsInfo, end_of_file)) {
  213. /* can not safely change the file size here if the
  214. client is writing to it due to potential races */
  215. i_size_write(tmp_inode, end_of_file);
  216. /* 512 bytes (2**9) is the fake blocksize that must be used */
  217. /* for this calculation, even though the reported blocksize is larger */
  218. tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9;
  219. }
  220. spin_unlock(&tmp_inode->i_lock);
  221. if (allocation_size < end_of_file)
  222. cFYI(1, ("May be sparse file, allocation less than file size"));
  223. cFYI(1, ("File Size %ld and blocks %llu",
  224. (unsigned long)tmp_inode->i_size,
  225. (unsigned long long)tmp_inode->i_blocks));
  226. if (S_ISREG(tmp_inode->i_mode)) {
  227. cFYI(1, ("File inode"));
  228. tmp_inode->i_op = &cifs_file_inode_ops;
  229. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  230. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  231. tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
  232. else
  233. tmp_inode->i_fop = &cifs_file_direct_ops;
  234. } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  235. tmp_inode->i_fop = &cifs_file_nobrl_ops;
  236. else
  237. tmp_inode->i_fop = &cifs_file_ops;
  238. if((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
  239. (cifs_sb->tcon->ses->server->maxBuf <
  240. PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
  241. tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
  242. else
  243. tmp_inode->i_data.a_ops = &cifs_addr_ops;
  244. if(isNewInode)
  245. return; /* No sense invalidating pages for new inode
  246. since have not started caching readahead file
  247. data yet */
  248. if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
  249. (local_size == tmp_inode->i_size)) {
  250. cFYI(1, ("inode exists but unchanged"));
  251. } else {
  252. /* file may have changed on server */
  253. cFYI(1, ("invalidate inode, readdir detected change"));
  254. invalidate_remote_inode(tmp_inode);
  255. }
  256. } else if (S_ISDIR(tmp_inode->i_mode)) {
  257. cFYI(1, ("Directory inode"));
  258. tmp_inode->i_op = &cifs_dir_inode_ops;
  259. tmp_inode->i_fop = &cifs_dir_ops;
  260. } else if (S_ISLNK(tmp_inode->i_mode)) {
  261. cFYI(1, ("Symbolic Link inode"));
  262. tmp_inode->i_op = &cifs_symlink_inode_ops;
  263. } else {
  264. cFYI(1, ("Init special inode"));
  265. init_special_inode(tmp_inode, tmp_inode->i_mode,
  266. tmp_inode->i_rdev);
  267. }
  268. }
  269. static void unix_fill_in_inode(struct inode *tmp_inode,
  270. FILE_UNIX_INFO *pfindData, int *pobject_type, int isNewInode)
  271. {
  272. loff_t local_size;
  273. struct timespec local_mtime;
  274. struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
  275. struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
  276. __u32 type = le32_to_cpu(pfindData->Type);
  277. __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes);
  278. __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
  279. cifsInfo->time = jiffies;
  280. atomic_inc(&cifsInfo->inUse);
  281. /* save mtime and size */
  282. local_mtime = tmp_inode->i_mtime;
  283. local_size = tmp_inode->i_size;
  284. tmp_inode->i_atime =
  285. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
  286. tmp_inode->i_mtime =
  287. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastModificationTime));
  288. tmp_inode->i_ctime =
  289. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastStatusChange));
  290. tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions);
  291. /* since we set the inode type below we need to mask off type
  292. to avoid strange results if bits above were corrupt */
  293. tmp_inode->i_mode &= ~S_IFMT;
  294. if (type == UNIX_FILE) {
  295. *pobject_type = DT_REG;
  296. tmp_inode->i_mode |= S_IFREG;
  297. } else if (type == UNIX_SYMLINK) {
  298. *pobject_type = DT_LNK;
  299. tmp_inode->i_mode |= S_IFLNK;
  300. } else if (type == UNIX_DIR) {
  301. *pobject_type = DT_DIR;
  302. tmp_inode->i_mode |= S_IFDIR;
  303. } else if (type == UNIX_CHARDEV) {
  304. *pobject_type = DT_CHR;
  305. tmp_inode->i_mode |= S_IFCHR;
  306. tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
  307. le64_to_cpu(pfindData->DevMinor) & MINORMASK);
  308. } else if (type == UNIX_BLOCKDEV) {
  309. *pobject_type = DT_BLK;
  310. tmp_inode->i_mode |= S_IFBLK;
  311. tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
  312. le64_to_cpu(pfindData->DevMinor) & MINORMASK);
  313. } else if (type == UNIX_FIFO) {
  314. *pobject_type = DT_FIFO;
  315. tmp_inode->i_mode |= S_IFIFO;
  316. } else if (type == UNIX_SOCKET) {
  317. *pobject_type = DT_SOCK;
  318. tmp_inode->i_mode |= S_IFSOCK;
  319. } else {
  320. /* safest to just call it a file */
  321. *pobject_type = DT_REG;
  322. tmp_inode->i_mode |= S_IFREG;
  323. cFYI(1,("unknown inode type %d",type));
  324. }
  325. tmp_inode->i_uid = le64_to_cpu(pfindData->Uid);
  326. tmp_inode->i_gid = le64_to_cpu(pfindData->Gid);
  327. tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks);
  328. spin_lock(&tmp_inode->i_lock);
  329. if (is_size_safe_to_change(cifsInfo, end_of_file)) {
  330. /* can not safely change the file size here if the
  331. client is writing to it due to potential races */
  332. i_size_write(tmp_inode, end_of_file);
  333. /* 512 bytes (2**9) is the fake blocksize that must be used */
  334. /* for this calculation, not the real blocksize */
  335. tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
  336. }
  337. spin_unlock(&tmp_inode->i_lock);
  338. if (S_ISREG(tmp_inode->i_mode)) {
  339. cFYI(1, ("File inode"));
  340. tmp_inode->i_op = &cifs_file_inode_ops;
  341. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  342. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  343. tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
  344. else
  345. tmp_inode->i_fop = &cifs_file_direct_ops;
  346. } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  347. tmp_inode->i_fop = &cifs_file_nobrl_ops;
  348. else
  349. tmp_inode->i_fop = &cifs_file_ops;
  350. if((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
  351. (cifs_sb->tcon->ses->server->maxBuf <
  352. PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
  353. tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
  354. else
  355. tmp_inode->i_data.a_ops = &cifs_addr_ops;
  356. if(isNewInode)
  357. return; /* No sense invalidating pages for new inode since we
  358. have not started caching readahead file data yet */
  359. if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
  360. (local_size == tmp_inode->i_size)) {
  361. cFYI(1, ("inode exists but unchanged"));
  362. } else {
  363. /* file may have changed on server */
  364. cFYI(1, ("invalidate inode, readdir detected change"));
  365. invalidate_remote_inode(tmp_inode);
  366. }
  367. } else if (S_ISDIR(tmp_inode->i_mode)) {
  368. cFYI(1, ("Directory inode"));
  369. tmp_inode->i_op = &cifs_dir_inode_ops;
  370. tmp_inode->i_fop = &cifs_dir_ops;
  371. } else if (S_ISLNK(tmp_inode->i_mode)) {
  372. cFYI(1, ("Symbolic Link inode"));
  373. tmp_inode->i_op = &cifs_symlink_inode_ops;
  374. /* tmp_inode->i_fop = *//* do not need to set to anything */
  375. } else {
  376. cFYI(1, ("Special inode"));
  377. init_special_inode(tmp_inode, tmp_inode->i_mode,
  378. tmp_inode->i_rdev);
  379. }
  380. }
  381. static int initiate_cifs_search(const int xid, struct file *file)
  382. {
  383. int rc = 0;
  384. char * full_path;
  385. struct cifsFileInfo * cifsFile;
  386. struct cifs_sb_info *cifs_sb;
  387. struct cifsTconInfo *pTcon;
  388. if(file->private_data == NULL) {
  389. file->private_data =
  390. kmalloc(sizeof(struct cifsFileInfo),GFP_KERNEL);
  391. }
  392. if(file->private_data == NULL) {
  393. return -ENOMEM;
  394. } else {
  395. memset(file->private_data,0,sizeof(struct cifsFileInfo));
  396. }
  397. cifsFile = file->private_data;
  398. cifsFile->invalidHandle = TRUE;
  399. cifsFile->srch_inf.endOfSearch = FALSE;
  400. if(file->f_path.dentry == NULL)
  401. return -ENOENT;
  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;
  556. struct cifsInodeInfo *cifsInfo;
  557. if(file->f_path.dentry == NULL)
  558. return 0;
  559. inode = file->f_path.dentry->d_inode;
  560. if(inode == NULL)
  561. return 0;
  562. cifsInfo = CIFS_I(inode);
  563. if(cifsInfo->time == 0)
  564. return 1; /* directory was changed, perhaps due to unlink */
  565. else
  566. return 0;
  567. }
  568. /* find the corresponding entry in the search */
  569. /* Note that the SMB server returns search entries for . and .. which
  570. complicates logic here if we choose to parse for them and we do not
  571. assume that they are located in the findfirst return buffer.*/
  572. /* We start counting in the buffer with entry 2 and increment for every
  573. entry (do not increment for . or .. entry) */
  574. static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
  575. struct file *file, char **ppCurrentEntry, int *num_to_ret)
  576. {
  577. int rc = 0;
  578. int pos_in_buf = 0;
  579. loff_t first_entry_in_buffer;
  580. loff_t index_to_find = file->f_pos;
  581. struct cifsFileInfo * cifsFile = file->private_data;
  582. /* check if index in the buffer */
  583. if((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
  584. (num_to_ret == NULL))
  585. return -ENOENT;
  586. *ppCurrentEntry = NULL;
  587. first_entry_in_buffer =
  588. cifsFile->srch_inf.index_of_last_entry -
  589. cifsFile->srch_inf.entries_in_buffer;
  590. /* if first entry in buf is zero then is first buffer
  591. in search response data which means it is likely . and ..
  592. will be in this buffer, although some servers do not return
  593. . and .. for the root of a drive and for those we need
  594. to start two entries earlier */
  595. #ifdef CONFIG_CIFS_DEBUG2
  596. dump_cifs_file_struct(file, "In fce ");
  597. #endif
  598. if(((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
  599. is_dir_changed(file)) ||
  600. (index_to_find < first_entry_in_buffer)) {
  601. /* close and restart search */
  602. cFYI(1,("search backing up - close and restart search"));
  603. cifsFile->invalidHandle = TRUE;
  604. CIFSFindClose(xid, pTcon, cifsFile->netfid);
  605. kfree(cifsFile->search_resume_name);
  606. cifsFile->search_resume_name = NULL;
  607. if(cifsFile->srch_inf.ntwrk_buf_start) {
  608. cFYI(1,("freeing SMB ff cache buf on search rewind"));
  609. if(cifsFile->srch_inf.smallBuf)
  610. cifs_small_buf_release(cifsFile->srch_inf.
  611. ntwrk_buf_start);
  612. else
  613. cifs_buf_release(cifsFile->srch_inf.
  614. ntwrk_buf_start);
  615. }
  616. rc = initiate_cifs_search(xid,file);
  617. if(rc) {
  618. cFYI(1,("error %d reinitiating a search on rewind",rc));
  619. return rc;
  620. }
  621. }
  622. while((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
  623. (rc == 0) && (cifsFile->srch_inf.endOfSearch == FALSE)){
  624. cFYI(1,("calling findnext2"));
  625. rc = CIFSFindNext(xid,pTcon,cifsFile->netfid,
  626. &cifsFile->srch_inf);
  627. if(rc)
  628. return -ENOENT;
  629. }
  630. if(index_to_find < cifsFile->srch_inf.index_of_last_entry) {
  631. /* we found the buffer that contains the entry */
  632. /* scan and find it */
  633. int i;
  634. char * current_entry;
  635. char * end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
  636. smbCalcSize((struct smb_hdr *)
  637. cifsFile->srch_inf.ntwrk_buf_start);
  638. current_entry = cifsFile->srch_inf.srch_entries_start;
  639. first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
  640. - cifsFile->srch_inf.entries_in_buffer;
  641. pos_in_buf = index_to_find - first_entry_in_buffer;
  642. cFYI(1,("found entry - pos_in_buf %d",pos_in_buf));
  643. for(i=0;(i<(pos_in_buf)) && (current_entry != NULL);i++) {
  644. /* go entry by entry figuring out which is first */
  645. current_entry = nxt_dir_entry(current_entry,end_of_smb,
  646. cifsFile->srch_inf.info_level);
  647. }
  648. if((current_entry == NULL) && (i < pos_in_buf)) {
  649. /* BB fixme - check if we should flag this error */
  650. cERROR(1,("reached end of buf searching for pos in buf"
  651. " %d index to find %lld rc %d",
  652. pos_in_buf,index_to_find,rc));
  653. }
  654. rc = 0;
  655. *ppCurrentEntry = current_entry;
  656. } else {
  657. cFYI(1,("index not in buffer - could not findnext into it"));
  658. return 0;
  659. }
  660. if(pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
  661. cFYI(1,("can not return entries pos_in_buf beyond last entry"));
  662. *num_to_ret = 0;
  663. } else
  664. *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
  665. return rc;
  666. }
  667. /* inode num, inode type and filename returned */
  668. static int cifs_get_name_from_search_buf(struct qstr *pqst,
  669. char *current_entry, __u16 level, unsigned int unicode,
  670. struct cifs_sb_info * cifs_sb, int max_len, ino_t *pinum)
  671. {
  672. int rc = 0;
  673. unsigned int len = 0;
  674. char * filename;
  675. struct nls_table * nlt = cifs_sb->local_nls;
  676. *pinum = 0;
  677. if(level == SMB_FIND_FILE_UNIX) {
  678. FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
  679. filename = &pFindData->FileName[0];
  680. if(unicode) {
  681. len = cifs_unicode_bytelen(filename);
  682. } else {
  683. /* BB should we make this strnlen of PATH_MAX? */
  684. len = strnlen(filename, PATH_MAX);
  685. }
  686. /* BB fixme - hash low and high 32 bits if not 64 bit arch BB fixme */
  687. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
  688. *pinum = pFindData->UniqueId;
  689. } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
  690. FILE_DIRECTORY_INFO * pFindData =
  691. (FILE_DIRECTORY_INFO *)current_entry;
  692. filename = &pFindData->FileName[0];
  693. len = le32_to_cpu(pFindData->FileNameLength);
  694. } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
  695. FILE_FULL_DIRECTORY_INFO * pFindData =
  696. (FILE_FULL_DIRECTORY_INFO *)current_entry;
  697. filename = &pFindData->FileName[0];
  698. len = le32_to_cpu(pFindData->FileNameLength);
  699. } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
  700. SEARCH_ID_FULL_DIR_INFO * pFindData =
  701. (SEARCH_ID_FULL_DIR_INFO *)current_entry;
  702. filename = &pFindData->FileName[0];
  703. len = le32_to_cpu(pFindData->FileNameLength);
  704. *pinum = pFindData->UniqueId;
  705. } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
  706. FILE_BOTH_DIRECTORY_INFO * pFindData =
  707. (FILE_BOTH_DIRECTORY_INFO *)current_entry;
  708. filename = &pFindData->FileName[0];
  709. len = le32_to_cpu(pFindData->FileNameLength);
  710. } else if(level == SMB_FIND_FILE_INFO_STANDARD) {
  711. FIND_FILE_STANDARD_INFO * pFindData =
  712. (FIND_FILE_STANDARD_INFO *)current_entry;
  713. filename = &pFindData->FileName[0];
  714. /* one byte length, no name conversion */
  715. len = (unsigned int)pFindData->FileNameLength;
  716. } else {
  717. cFYI(1,("Unknown findfirst level %d",level));
  718. return -EINVAL;
  719. }
  720. if(len > max_len) {
  721. cERROR(1,("bad search response length %d past smb end", len));
  722. return -EINVAL;
  723. }
  724. if(unicode) {
  725. /* BB fixme - test with long names */
  726. /* Note converted filename can be longer than in unicode */
  727. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
  728. pqst->len = cifs_convertUCSpath((char *)pqst->name,
  729. (__le16 *)filename, len/2, nlt);
  730. else
  731. pqst->len = cifs_strfromUCS_le((char *)pqst->name,
  732. (__le16 *)filename,len/2,nlt);
  733. } else {
  734. pqst->name = filename;
  735. pqst->len = len;
  736. }
  737. pqst->hash = full_name_hash(pqst->name,pqst->len);
  738. /* cFYI(1,("filldir on %s",pqst->name)); */
  739. return rc;
  740. }
  741. static int cifs_filldir(char *pfindEntry, struct file *file,
  742. filldir_t filldir, void *direntry, char *scratch_buf, int max_len)
  743. {
  744. int rc = 0;
  745. struct qstr qstring;
  746. struct cifsFileInfo * pCifsF;
  747. unsigned obj_type;
  748. ino_t inum;
  749. struct cifs_sb_info * cifs_sb;
  750. struct inode *tmp_inode;
  751. struct dentry *tmp_dentry;
  752. /* get filename and len into qstring */
  753. /* get dentry */
  754. /* decide whether to create and populate ionde */
  755. if((direntry == NULL) || (file == NULL))
  756. return -EINVAL;
  757. pCifsF = file->private_data;
  758. if((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
  759. return -ENOENT;
  760. if(file->f_path.dentry == NULL)
  761. return -ENOENT;
  762. rc = cifs_entry_is_dot(pfindEntry,pCifsF);
  763. /* skip . and .. since we added them first */
  764. if(rc != 0)
  765. return 0;
  766. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  767. qstring.name = scratch_buf;
  768. rc = cifs_get_name_from_search_buf(&qstring,pfindEntry,
  769. pCifsF->srch_inf.info_level,
  770. pCifsF->srch_inf.unicode,cifs_sb,
  771. max_len,
  772. &inum /* returned */);
  773. if(rc)
  774. return rc;
  775. rc = construct_dentry(&qstring,file,&tmp_inode, &tmp_dentry);
  776. if((tmp_inode == NULL) || (tmp_dentry == NULL))
  777. return -ENOMEM;
  778. if(rc) {
  779. /* inode created, we need to hash it with right inode number */
  780. if(inum != 0) {
  781. /* BB fixme - hash the 2 32 quantities bits together if necessary BB */
  782. tmp_inode->i_ino = inum;
  783. }
  784. insert_inode_hash(tmp_inode);
  785. }
  786. /* we pass in rc below, indicating whether it is a new inode,
  787. so we can figure out whether to invalidate the inode cached
  788. data if the file has changed */
  789. if(pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
  790. unix_fill_in_inode(tmp_inode,
  791. (FILE_UNIX_INFO *)pfindEntry,
  792. &obj_type, rc);
  793. else if(pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
  794. fill_in_inode(tmp_inode, 0 /* old level 1 buffer type */,
  795. pfindEntry, &obj_type, rc);
  796. else
  797. fill_in_inode(tmp_inode, 1 /* NT */, pfindEntry, &obj_type, rc);
  798. if(rc) /* new inode - needs to be tied to dentry */ {
  799. d_instantiate(tmp_dentry, tmp_inode);
  800. if(rc == 2)
  801. d_rehash(tmp_dentry);
  802. }
  803. rc = filldir(direntry,qstring.name,qstring.len,file->f_pos,
  804. tmp_inode->i_ino,obj_type);
  805. if(rc) {
  806. cFYI(1,("filldir rc = %d",rc));
  807. /* we can not return filldir errors to the caller
  808. since they are "normal" when the stat blocksize
  809. is too small - we return remapped error instead */
  810. rc = -EOVERFLOW;
  811. }
  812. dput(tmp_dentry);
  813. return rc;
  814. }
  815. static int cifs_save_resume_key(const char *current_entry,
  816. struct cifsFileInfo *cifsFile)
  817. {
  818. int rc = 0;
  819. unsigned int len = 0;
  820. __u16 level;
  821. char * filename;
  822. if((cifsFile == NULL) || (current_entry == NULL))
  823. return -EINVAL;
  824. level = cifsFile->srch_inf.info_level;
  825. if(level == SMB_FIND_FILE_UNIX) {
  826. FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
  827. filename = &pFindData->FileName[0];
  828. if(cifsFile->srch_inf.unicode) {
  829. len = cifs_unicode_bytelen(filename);
  830. } else {
  831. /* BB should we make this strnlen of PATH_MAX? */
  832. len = strnlen(filename, PATH_MAX);
  833. }
  834. cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
  835. } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
  836. FILE_DIRECTORY_INFO * pFindData =
  837. (FILE_DIRECTORY_INFO *)current_entry;
  838. filename = &pFindData->FileName[0];
  839. len = le32_to_cpu(pFindData->FileNameLength);
  840. cifsFile->srch_inf.resume_key = pFindData->FileIndex;
  841. } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
  842. FILE_FULL_DIRECTORY_INFO * pFindData =
  843. (FILE_FULL_DIRECTORY_INFO *)current_entry;
  844. filename = &pFindData->FileName[0];
  845. len = le32_to_cpu(pFindData->FileNameLength);
  846. cifsFile->srch_inf.resume_key = pFindData->FileIndex;
  847. } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
  848. SEARCH_ID_FULL_DIR_INFO * pFindData =
  849. (SEARCH_ID_FULL_DIR_INFO *)current_entry;
  850. filename = &pFindData->FileName[0];
  851. len = le32_to_cpu(pFindData->FileNameLength);
  852. cifsFile->srch_inf.resume_key = pFindData->FileIndex;
  853. } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
  854. FILE_BOTH_DIRECTORY_INFO * pFindData =
  855. (FILE_BOTH_DIRECTORY_INFO *)current_entry;
  856. filename = &pFindData->FileName[0];
  857. len = le32_to_cpu(pFindData->FileNameLength);
  858. cifsFile->srch_inf.resume_key = pFindData->FileIndex;
  859. } else if(level == SMB_FIND_FILE_INFO_STANDARD) {
  860. FIND_FILE_STANDARD_INFO * pFindData =
  861. (FIND_FILE_STANDARD_INFO *)current_entry;
  862. filename = &pFindData->FileName[0];
  863. /* one byte length, no name conversion */
  864. len = (unsigned int)pFindData->FileNameLength;
  865. cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
  866. } else {
  867. cFYI(1,("Unknown findfirst level %d",level));
  868. return -EINVAL;
  869. }
  870. cifsFile->srch_inf.resume_name_len = len;
  871. cifsFile->srch_inf.presume_name = filename;
  872. return rc;
  873. }
  874. int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
  875. {
  876. int rc = 0;
  877. int xid,i;
  878. struct cifs_sb_info *cifs_sb;
  879. struct cifsTconInfo *pTcon;
  880. struct cifsFileInfo *cifsFile = NULL;
  881. char * current_entry;
  882. int num_to_fill = 0;
  883. char * tmp_buf = NULL;
  884. char * end_of_smb;
  885. int max_len;
  886. xid = GetXid();
  887. if(file->f_path.dentry == NULL) {
  888. FreeXid(xid);
  889. return -EIO;
  890. }
  891. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  892. pTcon = cifs_sb->tcon;
  893. if(pTcon == NULL)
  894. return -EINVAL;
  895. switch ((int) file->f_pos) {
  896. case 0:
  897. if (filldir(direntry, ".", 1, file->f_pos,
  898. file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
  899. cERROR(1, ("Filldir for current dir failed"));
  900. rc = -ENOMEM;
  901. break;
  902. }
  903. file->f_pos++;
  904. case 1:
  905. if (filldir(direntry, "..", 2, file->f_pos,
  906. file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
  907. cERROR(1, ("Filldir for parent dir failed"));
  908. rc = -ENOMEM;
  909. break;
  910. }
  911. file->f_pos++;
  912. default:
  913. /* 1) If search is active,
  914. is in current search buffer?
  915. if it before then restart search
  916. if after then keep searching till find it */
  917. if(file->private_data == NULL) {
  918. rc = initiate_cifs_search(xid,file);
  919. cFYI(1,("initiate cifs search rc %d",rc));
  920. if(rc) {
  921. FreeXid(xid);
  922. return rc;
  923. }
  924. }
  925. if(file->private_data == NULL) {
  926. rc = -EINVAL;
  927. FreeXid(xid);
  928. return rc;
  929. }
  930. cifsFile = file->private_data;
  931. if (cifsFile->srch_inf.endOfSearch) {
  932. if(cifsFile->srch_inf.emptyDir) {
  933. cFYI(1, ("End of search, empty dir"));
  934. rc = 0;
  935. break;
  936. }
  937. } /* else {
  938. cifsFile->invalidHandle = TRUE;
  939. CIFSFindClose(xid, pTcon, cifsFile->netfid);
  940. }
  941. kfree(cifsFile->search_resume_name);
  942. cifsFile->search_resume_name = NULL; */
  943. rc = find_cifs_entry(xid,pTcon, file,
  944. &current_entry,&num_to_fill);
  945. if(rc) {
  946. cFYI(1,("fce error %d",rc));
  947. goto rddir2_exit;
  948. } else if (current_entry != NULL) {
  949. cFYI(1,("entry %lld found",file->f_pos));
  950. } else {
  951. cFYI(1,("could not find entry"));
  952. goto rddir2_exit;
  953. }
  954. cFYI(1,("loop through %d times filling dir for net buf %p",
  955. num_to_fill,cifsFile->srch_inf.ntwrk_buf_start));
  956. max_len = smbCalcSize((struct smb_hdr *)
  957. cifsFile->srch_inf.ntwrk_buf_start);
  958. end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
  959. /* To be safe - for UCS to UTF-8 with strings loaded
  960. with the rare long characters alloc more to account for
  961. such multibyte target UTF-8 characters. cifs_unicode.c,
  962. which actually does the conversion, has the same limit */
  963. tmp_buf = kmalloc((2 * NAME_MAX) + 4, GFP_KERNEL);
  964. for(i=0;(i<num_to_fill) && (rc == 0);i++) {
  965. if(current_entry == NULL) {
  966. /* evaluate whether this case is an error */
  967. cERROR(1,("past end of SMB num to fill %d i %d",
  968. num_to_fill, i));
  969. break;
  970. }
  971. /* if buggy server returns . and .. late do
  972. we want to check for that here? */
  973. rc = cifs_filldir(current_entry, file,
  974. filldir, direntry, tmp_buf, max_len);
  975. if(rc == -EOVERFLOW) {
  976. rc = 0;
  977. break;
  978. }
  979. file->f_pos++;
  980. if(file->f_pos ==
  981. cifsFile->srch_inf.index_of_last_entry) {
  982. cFYI(1,("last entry in buf at pos %lld %s",
  983. file->f_pos,tmp_buf));
  984. cifs_save_resume_key(current_entry,cifsFile);
  985. break;
  986. } else
  987. current_entry =
  988. nxt_dir_entry(current_entry, end_of_smb,
  989. cifsFile->srch_inf.info_level);
  990. }
  991. kfree(tmp_buf);
  992. break;
  993. } /* end switch */
  994. rddir2_exit:
  995. FreeXid(xid);
  996. return rc;
  997. }