readdir.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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",
  71. 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. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
  326. tmp_inode->i_uid = cifs_sb->mnt_uid;
  327. else
  328. tmp_inode->i_uid = le64_to_cpu(pfindData->Uid);
  329. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
  330. tmp_inode->i_gid = cifs_sb->mnt_gid;
  331. else
  332. tmp_inode->i_gid = le64_to_cpu(pfindData->Gid);
  333. tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks);
  334. spin_lock(&tmp_inode->i_lock);
  335. if (is_size_safe_to_change(cifsInfo, end_of_file)) {
  336. /* can not safely change the file size here if the
  337. client is writing to it due to potential races */
  338. i_size_write(tmp_inode, end_of_file);
  339. /* 512 bytes (2**9) is the fake blocksize that must be used */
  340. /* for this calculation, not the real blocksize */
  341. tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
  342. }
  343. spin_unlock(&tmp_inode->i_lock);
  344. if (S_ISREG(tmp_inode->i_mode)) {
  345. cFYI(1, ("File inode"));
  346. tmp_inode->i_op = &cifs_file_inode_ops;
  347. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  348. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  349. tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
  350. else
  351. tmp_inode->i_fop = &cifs_file_direct_ops;
  352. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  353. tmp_inode->i_fop = &cifs_file_nobrl_ops;
  354. else
  355. tmp_inode->i_fop = &cifs_file_ops;
  356. if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
  357. (cifs_sb->tcon->ses->server->maxBuf <
  358. PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
  359. tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
  360. else
  361. tmp_inode->i_data.a_ops = &cifs_addr_ops;
  362. if (isNewInode)
  363. return; /* No sense invalidating pages for new inode
  364. since we have not started caching readahead
  365. file data for it yet */
  366. if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
  367. (local_size == tmp_inode->i_size)) {
  368. cFYI(1, ("inode exists but unchanged"));
  369. } else {
  370. /* file may have changed on server */
  371. cFYI(1, ("invalidate inode, readdir detected change"));
  372. invalidate_remote_inode(tmp_inode);
  373. }
  374. } else if (S_ISDIR(tmp_inode->i_mode)) {
  375. cFYI(1, ("Directory inode"));
  376. tmp_inode->i_op = &cifs_dir_inode_ops;
  377. tmp_inode->i_fop = &cifs_dir_ops;
  378. } else if (S_ISLNK(tmp_inode->i_mode)) {
  379. cFYI(1, ("Symbolic Link inode"));
  380. tmp_inode->i_op = &cifs_symlink_inode_ops;
  381. /* tmp_inode->i_fop = *//* do not need to set to anything */
  382. } else {
  383. cFYI(1, ("Special inode"));
  384. init_special_inode(tmp_inode, tmp_inode->i_mode,
  385. tmp_inode->i_rdev);
  386. }
  387. }
  388. static int initiate_cifs_search(const int xid, struct file *file)
  389. {
  390. int rc = 0;
  391. char *full_path;
  392. struct cifsFileInfo *cifsFile;
  393. struct cifs_sb_info *cifs_sb;
  394. struct cifsTconInfo *pTcon;
  395. if (file->private_data == NULL) {
  396. file->private_data =
  397. kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
  398. }
  399. if (file->private_data == NULL)
  400. return -ENOMEM;
  401. cifsFile = file->private_data;
  402. cifsFile->invalidHandle = TRUE;
  403. cifsFile->srch_inf.endOfSearch = FALSE;
  404. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  405. if (cifs_sb == NULL)
  406. return -EINVAL;
  407. pTcon = cifs_sb->tcon;
  408. if (pTcon == NULL)
  409. return -EINVAL;
  410. full_path = build_path_from_dentry(file->f_path.dentry);
  411. if (full_path == NULL) {
  412. return -ENOMEM;
  413. }
  414. cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
  415. ffirst_retry:
  416. /* test for Unix extensions */
  417. /* but now check for them on the share/mount not on the SMB session */
  418. /* if (pTcon->ses->capabilities & CAP_UNIX) { */
  419. if (pTcon->unix_ext) {
  420. cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
  421. } else if ((pTcon->ses->capabilities &
  422. (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
  423. cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
  424. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  425. cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
  426. } else /* not srvinos - BB fixme add check for backlevel? */ {
  427. cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
  428. }
  429. rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
  430. &cifsFile->netfid, &cifsFile->srch_inf,
  431. cifs_sb->mnt_cifs_flags &
  432. CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
  433. if (rc == 0)
  434. cifsFile->invalidHandle = FALSE;
  435. if ((rc == -EOPNOTSUPP) &&
  436. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
  437. cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
  438. goto ffirst_retry;
  439. }
  440. kfree(full_path);
  441. return rc;
  442. }
  443. /* return length of unicode string in bytes */
  444. static int cifs_unicode_bytelen(char *str)
  445. {
  446. int len;
  447. __le16 * ustr = (__le16 *)str;
  448. for (len = 0; len <= PATH_MAX; len++) {
  449. if (ustr[len] == 0)
  450. return len << 1;
  451. }
  452. cFYI(1, ("Unicode string longer than PATH_MAX found"));
  453. return len << 1;
  454. }
  455. static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
  456. {
  457. char *new_entry;
  458. FILE_DIRECTORY_INFO * pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
  459. if (level == SMB_FIND_FILE_INFO_STANDARD) {
  460. FIND_FILE_STANDARD_INFO * pfData;
  461. pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
  462. new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
  463. pfData->FileNameLength;
  464. } else
  465. new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
  466. cFYI(1, ("new entry %p old entry %p", new_entry, old_entry));
  467. /* validate that new_entry is not past end of SMB */
  468. if (new_entry >= end_of_smb) {
  469. cERROR(1,
  470. ("search entry %p began after end of SMB %p old entry %p",
  471. new_entry, end_of_smb, old_entry));
  472. return NULL;
  473. } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
  474. (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
  475. || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
  476. (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
  477. cERROR(1, ("search entry %p extends after end of SMB %p",
  478. new_entry, end_of_smb));
  479. return NULL;
  480. } else
  481. return new_entry;
  482. }
  483. #define UNICODE_DOT cpu_to_le16(0x2e)
  484. /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
  485. static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
  486. {
  487. int rc = 0;
  488. char *filename = NULL;
  489. int len = 0;
  490. if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
  491. FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
  492. filename = &pFindData->FileName[0];
  493. if (cfile->srch_inf.unicode) {
  494. len = cifs_unicode_bytelen(filename);
  495. } else {
  496. /* BB should we make this strnlen of PATH_MAX? */
  497. len = strnlen(filename, 5);
  498. }
  499. } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
  500. FILE_DIRECTORY_INFO * pFindData =
  501. (FILE_DIRECTORY_INFO *)current_entry;
  502. filename = &pFindData->FileName[0];
  503. len = le32_to_cpu(pFindData->FileNameLength);
  504. } else if (cfile->srch_inf.info_level ==
  505. SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
  506. FILE_FULL_DIRECTORY_INFO * pFindData =
  507. (FILE_FULL_DIRECTORY_INFO *)current_entry;
  508. filename = &pFindData->FileName[0];
  509. len = le32_to_cpu(pFindData->FileNameLength);
  510. } else if (cfile->srch_inf.info_level ==
  511. SMB_FIND_FILE_ID_FULL_DIR_INFO) {
  512. SEARCH_ID_FULL_DIR_INFO * pFindData =
  513. (SEARCH_ID_FULL_DIR_INFO *)current_entry;
  514. filename = &pFindData->FileName[0];
  515. len = le32_to_cpu(pFindData->FileNameLength);
  516. } else if (cfile->srch_inf.info_level ==
  517. SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
  518. FILE_BOTH_DIRECTORY_INFO * pFindData =
  519. (FILE_BOTH_DIRECTORY_INFO *)current_entry;
  520. filename = &pFindData->FileName[0];
  521. len = le32_to_cpu(pFindData->FileNameLength);
  522. } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
  523. FIND_FILE_STANDARD_INFO * pFindData =
  524. (FIND_FILE_STANDARD_INFO *)current_entry;
  525. filename = &pFindData->FileName[0];
  526. len = pFindData->FileNameLength;
  527. } else {
  528. cFYI(1, ("Unknown findfirst level %d",
  529. cfile->srch_inf.info_level));
  530. }
  531. if (filename) {
  532. if (cfile->srch_inf.unicode) {
  533. __le16 *ufilename = (__le16 *)filename;
  534. if (len == 2) {
  535. /* check for . */
  536. if (ufilename[0] == UNICODE_DOT)
  537. rc = 1;
  538. } else if (len == 4) {
  539. /* check for .. */
  540. if ((ufilename[0] == UNICODE_DOT)
  541. && (ufilename[1] == UNICODE_DOT))
  542. rc = 2;
  543. }
  544. } else /* ASCII */ {
  545. if (len == 1) {
  546. if (filename[0] == '.')
  547. rc = 1;
  548. } else if (len == 2) {
  549. if ((filename[0] == '.') && (filename[1] == '.'))
  550. rc = 2;
  551. }
  552. }
  553. }
  554. return rc;
  555. }
  556. /* Check if directory that we are searching has changed so we can decide
  557. whether we can use the cached search results from the previous search */
  558. static int is_dir_changed(struct file *file)
  559. {
  560. struct inode *inode = file->f_path.dentry->d_inode;
  561. struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
  562. if (cifsInfo->time == 0)
  563. return 1; /* directory was changed, perhaps due to unlink */
  564. else
  565. return 0;
  566. }
  567. /* find the corresponding entry in the search */
  568. /* Note that the SMB server returns search entries for . and .. which
  569. complicates logic here if we choose to parse for them and we do not
  570. assume that they are located in the findfirst return buffer.*/
  571. /* We start counting in the buffer with entry 2 and increment for every
  572. entry (do not increment for . or .. entry) */
  573. static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
  574. struct file *file, char **ppCurrentEntry, int *num_to_ret)
  575. {
  576. int rc = 0;
  577. int pos_in_buf = 0;
  578. loff_t first_entry_in_buffer;
  579. loff_t index_to_find = file->f_pos;
  580. struct cifsFileInfo *cifsFile = file->private_data;
  581. /* check if index in the buffer */
  582. if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
  583. (num_to_ret == NULL))
  584. return -ENOENT;
  585. *ppCurrentEntry = NULL;
  586. first_entry_in_buffer =
  587. cifsFile->srch_inf.index_of_last_entry -
  588. cifsFile->srch_inf.entries_in_buffer;
  589. /* if first entry in buf is zero then is first buffer
  590. in search response data which means it is likely . and ..
  591. will be in this buffer, although some servers do not return
  592. . and .. for the root of a drive and for those we need
  593. to start two entries earlier */
  594. #ifdef CONFIG_CIFS_DEBUG2
  595. dump_cifs_file_struct(file, "In fce ");
  596. #endif
  597. if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
  598. is_dir_changed(file)) ||
  599. (index_to_find < first_entry_in_buffer)) {
  600. /* close and restart search */
  601. cFYI(1, ("search backing up - close and restart search"));
  602. cifsFile->invalidHandle = TRUE;
  603. CIFSFindClose(xid, pTcon, cifsFile->netfid);
  604. kfree(cifsFile->search_resume_name);
  605. cifsFile->search_resume_name = NULL;
  606. if (cifsFile->srch_inf.ntwrk_buf_start) {
  607. cFYI(1, ("freeing SMB ff cache buf on search rewind"));
  608. if (cifsFile->srch_inf.smallBuf)
  609. cifs_small_buf_release(cifsFile->srch_inf.
  610. ntwrk_buf_start);
  611. else
  612. cifs_buf_release(cifsFile->srch_inf.
  613. ntwrk_buf_start);
  614. }
  615. rc = initiate_cifs_search(xid, file);
  616. if (rc) {
  617. cFYI(1, ("error %d reinitiating a search on rewind",
  618. 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"));
  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 */
  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. rc = cifs_entry_is_dot(pfindEntry, pCifsF);
  761. /* skip . and .. since we added them first */
  762. if (rc != 0)
  763. return 0;
  764. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  765. qstring.name = scratch_buf;
  766. rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
  767. pCifsF->srch_inf.info_level,
  768. pCifsF->srch_inf.unicode, cifs_sb,
  769. max_len,
  770. &inum /* returned */);
  771. if (rc)
  772. return rc;
  773. rc = construct_dentry(&qstring, file, &tmp_inode, &tmp_dentry);
  774. if ((tmp_inode == NULL) || (tmp_dentry == NULL))
  775. return -ENOMEM;
  776. if (rc) {
  777. /* inode created, we need to hash it with right inode number */
  778. if (inum != 0) {
  779. /* BB fixme - hash the 2 32 quantities bits together if
  780. * necessary BB */
  781. tmp_inode->i_ino = inum;
  782. }
  783. insert_inode_hash(tmp_inode);
  784. }
  785. /* we pass in rc below, indicating whether it is a new inode,
  786. so we can figure out whether to invalidate the inode cached
  787. data if the file has changed */
  788. if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
  789. unix_fill_in_inode(tmp_inode,
  790. (FILE_UNIX_INFO *)pfindEntry,
  791. &obj_type, rc);
  792. else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
  793. fill_in_inode(tmp_inode, 0 /* old level 1 buffer type */,
  794. pfindEntry, &obj_type, rc);
  795. else
  796. fill_in_inode(tmp_inode, 1 /* NT */, pfindEntry, &obj_type, rc);
  797. if (rc) /* new inode - needs to be tied to dentry */ {
  798. d_instantiate(tmp_dentry, tmp_inode);
  799. if (rc == 2)
  800. d_rehash(tmp_dentry);
  801. }
  802. rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
  803. tmp_inode->i_ino, obj_type);
  804. if (rc) {
  805. cFYI(1, ("filldir rc = %d", rc));
  806. /* we can not return filldir errors to the caller
  807. since they are "normal" when the stat blocksize
  808. is too small - we return remapped error instead */
  809. rc = -EOVERFLOW;
  810. }
  811. dput(tmp_dentry);
  812. return rc;
  813. }
  814. static int cifs_save_resume_key(const char *current_entry,
  815. struct cifsFileInfo *cifsFile)
  816. {
  817. int rc = 0;
  818. unsigned int len = 0;
  819. __u16 level;
  820. char *filename;
  821. if ((cifsFile == NULL) || (current_entry == NULL))
  822. return -EINVAL;
  823. level = cifsFile->srch_inf.info_level;
  824. if (level == SMB_FIND_FILE_UNIX) {
  825. FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
  826. filename = &pFindData->FileName[0];
  827. if (cifsFile->srch_inf.unicode) {
  828. len = cifs_unicode_bytelen(filename);
  829. } else {
  830. /* BB should we make this strnlen of PATH_MAX? */
  831. len = strnlen(filename, PATH_MAX);
  832. }
  833. cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
  834. } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
  835. FILE_DIRECTORY_INFO *pFindData =
  836. (FILE_DIRECTORY_INFO *)current_entry;
  837. filename = &pFindData->FileName[0];
  838. len = le32_to_cpu(pFindData->FileNameLength);
  839. cifsFile->srch_inf.resume_key = pFindData->FileIndex;
  840. } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
  841. FILE_FULL_DIRECTORY_INFO *pFindData =
  842. (FILE_FULL_DIRECTORY_INFO *)current_entry;
  843. filename = &pFindData->FileName[0];
  844. len = le32_to_cpu(pFindData->FileNameLength);
  845. cifsFile->srch_inf.resume_key = pFindData->FileIndex;
  846. } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
  847. SEARCH_ID_FULL_DIR_INFO *pFindData =
  848. (SEARCH_ID_FULL_DIR_INFO *)current_entry;
  849. filename = &pFindData->FileName[0];
  850. len = le32_to_cpu(pFindData->FileNameLength);
  851. cifsFile->srch_inf.resume_key = pFindData->FileIndex;
  852. } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
  853. FILE_BOTH_DIRECTORY_INFO *pFindData =
  854. (FILE_BOTH_DIRECTORY_INFO *)current_entry;
  855. filename = &pFindData->FileName[0];
  856. len = le32_to_cpu(pFindData->FileNameLength);
  857. cifsFile->srch_inf.resume_key = pFindData->FileIndex;
  858. } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
  859. FIND_FILE_STANDARD_INFO *pFindData =
  860. (FIND_FILE_STANDARD_INFO *)current_entry;
  861. filename = &pFindData->FileName[0];
  862. /* one byte length, no name conversion */
  863. len = (unsigned int)pFindData->FileNameLength;
  864. cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
  865. } else {
  866. cFYI(1, ("Unknown findfirst level %d", level));
  867. return -EINVAL;
  868. }
  869. cifsFile->srch_inf.resume_name_len = len;
  870. cifsFile->srch_inf.presume_name = filename;
  871. return rc;
  872. }
  873. int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
  874. {
  875. int rc = 0;
  876. int xid, i;
  877. struct cifs_sb_info *cifs_sb;
  878. struct cifsTconInfo *pTcon;
  879. struct cifsFileInfo *cifsFile = NULL;
  880. char *current_entry;
  881. int num_to_fill = 0;
  882. char *tmp_buf = NULL;
  883. char *end_of_smb;
  884. int max_len;
  885. xid = GetXid();
  886. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  887. pTcon = cifs_sb->tcon;
  888. if (pTcon == NULL)
  889. return -EINVAL;
  890. switch ((int) file->f_pos) {
  891. case 0:
  892. if (filldir(direntry, ".", 1, file->f_pos,
  893. file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
  894. cERROR(1, ("Filldir for current dir failed"));
  895. rc = -ENOMEM;
  896. break;
  897. }
  898. file->f_pos++;
  899. case 1:
  900. if (filldir(direntry, "..", 2, file->f_pos,
  901. file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
  902. cERROR(1, ("Filldir for parent dir failed"));
  903. rc = -ENOMEM;
  904. break;
  905. }
  906. file->f_pos++;
  907. default:
  908. /* 1) If search is active,
  909. is in current search buffer?
  910. if it before then restart search
  911. if after then keep searching till find it */
  912. if (file->private_data == NULL) {
  913. rc = initiate_cifs_search(xid, file);
  914. cFYI(1, ("initiate cifs search rc %d", rc));
  915. if (rc) {
  916. FreeXid(xid);
  917. return rc;
  918. }
  919. }
  920. if (file->private_data == NULL) {
  921. rc = -EINVAL;
  922. FreeXid(xid);
  923. return rc;
  924. }
  925. cifsFile = file->private_data;
  926. if (cifsFile->srch_inf.endOfSearch) {
  927. if (cifsFile->srch_inf.emptyDir) {
  928. cFYI(1, ("End of search, empty dir"));
  929. rc = 0;
  930. break;
  931. }
  932. } /* else {
  933. cifsFile->invalidHandle = TRUE;
  934. CIFSFindClose(xid, pTcon, cifsFile->netfid);
  935. }
  936. kfree(cifsFile->search_resume_name);
  937. cifsFile->search_resume_name = NULL; */
  938. rc = find_cifs_entry(xid, pTcon, file,
  939. &current_entry, &num_to_fill);
  940. if (rc) {
  941. cFYI(1, ("fce error %d", rc));
  942. goto rddir2_exit;
  943. } else if (current_entry != NULL) {
  944. cFYI(1, ("entry %lld found", file->f_pos));
  945. } else {
  946. cFYI(1, ("could not find entry"));
  947. goto rddir2_exit;
  948. }
  949. cFYI(1, ("loop through %d times filling dir for net buf %p",
  950. num_to_fill, cifsFile->srch_inf.ntwrk_buf_start));
  951. max_len = smbCalcSize((struct smb_hdr *)
  952. cifsFile->srch_inf.ntwrk_buf_start);
  953. end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
  954. /* To be safe - for UCS to UTF-8 with strings loaded
  955. with the rare long characters alloc more to account for
  956. such multibyte target UTF-8 characters. cifs_unicode.c,
  957. which actually does the conversion, has the same limit */
  958. tmp_buf = kmalloc((2 * NAME_MAX) + 4, GFP_KERNEL);
  959. for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
  960. if (current_entry == NULL) {
  961. /* evaluate whether this case is an error */
  962. cERROR(1,("past end of SMB num to fill %d i %d",
  963. num_to_fill, i));
  964. break;
  965. }
  966. /* if buggy server returns . and .. late do
  967. we want to check for that here? */
  968. rc = cifs_filldir(current_entry, file,
  969. filldir, direntry, tmp_buf, max_len);
  970. if (rc == -EOVERFLOW) {
  971. rc = 0;
  972. break;
  973. }
  974. file->f_pos++;
  975. if (file->f_pos ==
  976. cifsFile->srch_inf.index_of_last_entry) {
  977. cFYI(1, ("last entry in buf at pos %lld %s",
  978. file->f_pos, tmp_buf));
  979. cifs_save_resume_key(current_entry, cifsFile);
  980. break;
  981. } else
  982. current_entry =
  983. nxt_dir_entry(current_entry, end_of_smb,
  984. cifsFile->srch_inf.info_level);
  985. }
  986. kfree(tmp_buf);
  987. break;
  988. } /* end switch */
  989. rddir2_exit:
  990. FreeXid(xid);
  991. return rc;
  992. }