readdir.c 34 KB

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