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, 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. #endif /* DEBUG2 */
  52. /* Returns one if new inode created (which therefore needs to be hashed) */
  53. /* Might check in the future if inode number changed so we can rehash inode */
  54. static int construct_dentry(struct qstr *qstring, struct file *file,
  55. struct inode **ptmp_inode, struct dentry **pnew_dentry)
  56. {
  57. struct dentry *tmp_dentry;
  58. struct cifs_sb_info *cifs_sb;
  59. struct cifsTconInfo *pTcon;
  60. int rc = 0;
  61. cFYI(1, ("For %s", qstring->name));
  62. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  63. pTcon = cifs_sb->tcon;
  64. qstring->hash = full_name_hash(qstring->name, qstring->len);
  65. tmp_dentry = d_lookup(file->f_path.dentry, qstring);
  66. if (tmp_dentry) {
  67. cFYI(0, ("existing dentry with inode 0x%p",
  68. tmp_dentry->d_inode));
  69. *ptmp_inode = tmp_dentry->d_inode;
  70. /* BB overwrite old name? i.e. tmp_dentry->d_name and tmp_dentry->d_name.len??*/
  71. if (*ptmp_inode == NULL) {
  72. *ptmp_inode = new_inode(file->f_path.dentry->d_sb);
  73. if (*ptmp_inode == NULL)
  74. return rc;
  75. rc = 1;
  76. }
  77. if (file->f_path.dentry->d_sb->s_flags & MS_NOATIME)
  78. (*ptmp_inode)->i_flags |= S_NOATIME | S_NOCMTIME;
  79. } else {
  80. tmp_dentry = d_alloc(file->f_path.dentry, qstring);
  81. if (tmp_dentry == NULL) {
  82. cERROR(1, ("Failed allocating dentry"));
  83. *ptmp_inode = NULL;
  84. return rc;
  85. }
  86. *ptmp_inode = new_inode(file->f_path.dentry->d_sb);
  87. if (pTcon->nocase)
  88. tmp_dentry->d_op = &cifs_ci_dentry_ops;
  89. else
  90. tmp_dentry->d_op = &cifs_dentry_ops;
  91. if (*ptmp_inode == NULL)
  92. return rc;
  93. if (file->f_path.dentry->d_sb->s_flags & MS_NOATIME)
  94. (*ptmp_inode)->i_flags |= S_NOATIME | S_NOCMTIME;
  95. rc = 2;
  96. }
  97. tmp_dentry->d_time = jiffies;
  98. *pnew_dentry = tmp_dentry;
  99. return rc;
  100. }
  101. static void AdjustForTZ(struct cifsTconInfo *tcon, struct inode *inode)
  102. {
  103. if ((tcon) && (tcon->ses) && (tcon->ses->server)) {
  104. inode->i_ctime.tv_sec += tcon->ses->server->timeAdj;
  105. inode->i_mtime.tv_sec += tcon->ses->server->timeAdj;
  106. inode->i_atime.tv_sec += tcon->ses->server->timeAdj;
  107. }
  108. return;
  109. }
  110. static void fill_in_inode(struct inode *tmp_inode, int new_buf_type,
  111. char *buf, unsigned int *pobject_type, int isNewInode)
  112. {
  113. loff_t local_size;
  114. struct timespec local_mtime;
  115. struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
  116. struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
  117. __u32 attr;
  118. __u64 allocation_size;
  119. __u64 end_of_file;
  120. /* save mtime and size */
  121. local_mtime = tmp_inode->i_mtime;
  122. local_size = tmp_inode->i_size;
  123. if (new_buf_type) {
  124. FILE_DIRECTORY_INFO *pfindData = (FILE_DIRECTORY_INFO *)buf;
  125. attr = le32_to_cpu(pfindData->ExtFileAttributes);
  126. allocation_size = le64_to_cpu(pfindData->AllocationSize);
  127. end_of_file = le64_to_cpu(pfindData->EndOfFile);
  128. tmp_inode->i_atime =
  129. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
  130. tmp_inode->i_mtime =
  131. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
  132. tmp_inode->i_ctime =
  133. cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
  134. } else { /* legacy, OS2 and DOS style */
  135. /* struct timespec ts;*/
  136. FIND_FILE_STANDARD_INFO *pfindData =
  137. (FIND_FILE_STANDARD_INFO *)buf;
  138. tmp_inode->i_mtime = cnvrtDosUnixTm(
  139. le16_to_cpu(pfindData->LastWriteDate),
  140. le16_to_cpu(pfindData->LastWriteTime));
  141. tmp_inode->i_atime = cnvrtDosUnixTm(
  142. le16_to_cpu(pfindData->LastAccessDate),
  143. le16_to_cpu(pfindData->LastAccessTime));
  144. tmp_inode->i_ctime = cnvrtDosUnixTm(
  145. le16_to_cpu(pfindData->LastWriteDate),
  146. le16_to_cpu(pfindData->LastWriteTime));
  147. AdjustForTZ(cifs_sb->tcon, tmp_inode);
  148. attr = le16_to_cpu(pfindData->Attributes);
  149. allocation_size = le32_to_cpu(pfindData->AllocationSize);
  150. end_of_file = le32_to_cpu(pfindData->DataSize);
  151. }
  152. /* Linux can not store file creation time unfortunately so ignore it */
  153. cifsInfo->cifsAttrs = attr;
  154. #ifdef CONFIG_CIFS_EXPERIMENTAL
  155. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
  156. /* get more accurate mode via ACL - so force inode refresh */
  157. cifsInfo->time = 0;
  158. } else
  159. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  160. cifsInfo->time = jiffies;
  161. /* treat dos attribute of read-only as read-only mode bit e.g. 555? */
  162. /* 2767 perms - indicate mandatory locking */
  163. /* BB fill in uid and gid here? with help from winbind?
  164. or retrieve from NTFS stream extended attribute */
  165. if (atomic_read(&cifsInfo->inUse) == 0) {
  166. tmp_inode->i_uid = cifs_sb->mnt_uid;
  167. tmp_inode->i_gid = cifs_sb->mnt_gid;
  168. /* set default mode. will override for dirs below */
  169. tmp_inode->i_mode = cifs_sb->mnt_file_mode;
  170. } else {
  171. /* mask off the type bits since it gets set
  172. below and we do not want to get two type
  173. bits set */
  174. tmp_inode->i_mode &= ~S_IFMT;
  175. }
  176. if (attr & ATTR_DIRECTORY) {
  177. *pobject_type = DT_DIR;
  178. /* override default perms since we do not lock dirs */
  179. if (atomic_read(&cifsInfo->inUse) == 0)
  180. tmp_inode->i_mode = cifs_sb->mnt_dir_mode;
  181. tmp_inode->i_mode |= S_IFDIR;
  182. } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
  183. (attr & ATTR_SYSTEM)) {
  184. if (end_of_file == 0) {
  185. *pobject_type = DT_FIFO;
  186. tmp_inode->i_mode |= S_IFIFO;
  187. } else {
  188. /* rather than get the type here, we mark the
  189. inode as needing revalidate and get the real type
  190. (blk vs chr vs. symlink) later ie in lookup */
  191. *pobject_type = DT_REG;
  192. tmp_inode->i_mode |= S_IFREG;
  193. cifsInfo->time = 0;
  194. }
  195. /* we no longer mark these because we could not follow them */
  196. /* } else if (attr & ATTR_REPARSE) {
  197. *pobject_type = DT_LNK;
  198. tmp_inode->i_mode |= S_IFLNK; */
  199. } else {
  200. *pobject_type = DT_REG;
  201. tmp_inode->i_mode |= S_IFREG;
  202. if (attr & ATTR_READONLY)
  203. tmp_inode->i_mode &= ~(S_IWUGO);
  204. else if ((tmp_inode->i_mode & S_IWUGO) == 0)
  205. /* the ATTR_READONLY flag may have been changed on */
  206. /* server -- set any w bits allowed by mnt_file_mode */
  207. tmp_inode->i_mode |= (S_IWUGO & cifs_sb->mnt_file_mode);
  208. } /* could add code here - to validate if device or weird share type? */
  209. /* can not fill in nlink here as in qpathinfo version and Unx search */
  210. if (atomic_read(&cifsInfo->inUse) == 0)
  211. atomic_set(&cifsInfo->inUse, 1);
  212. spin_lock(&tmp_inode->i_lock);
  213. if (is_size_safe_to_change(cifsInfo, end_of_file)) {
  214. /* can not safely change the file size here if the
  215. client is writing to it due to potential races */
  216. i_size_write(tmp_inode, end_of_file);
  217. /* 512 bytes (2**9) is the fake blocksize that must be used */
  218. /* for this calculation, even though the reported blocksize is larger */
  219. tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9;
  220. }
  221. spin_unlock(&tmp_inode->i_lock);
  222. if (allocation_size < end_of_file)
  223. cFYI(1, ("May be sparse file, allocation less than file size"));
  224. cFYI(1, ("File Size %ld and blocks %llu",
  225. (unsigned long)tmp_inode->i_size,
  226. (unsigned long long)tmp_inode->i_blocks));
  227. if (S_ISREG(tmp_inode->i_mode)) {
  228. cFYI(1, ("File inode"));
  229. tmp_inode->i_op = &cifs_file_inode_ops;
  230. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  231. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  232. tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
  233. else
  234. tmp_inode->i_fop = &cifs_file_direct_ops;
  235. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  236. tmp_inode->i_fop = &cifs_file_nobrl_ops;
  237. else
  238. tmp_inode->i_fop = &cifs_file_ops;
  239. if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
  240. (cifs_sb->tcon->ses->server->maxBuf <
  241. PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
  242. tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
  243. else
  244. tmp_inode->i_data.a_ops = &cifs_addr_ops;
  245. if (isNewInode)
  246. return; /* No sense invalidating pages for new inode
  247. since have not started caching readahead file
  248. data yet */
  249. if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
  250. (local_size == tmp_inode->i_size)) {
  251. cFYI(1, ("inode exists but unchanged"));
  252. } else {
  253. /* file may have changed on server */
  254. cFYI(1, ("invalidate inode, readdir detected change"));
  255. invalidate_remote_inode(tmp_inode);
  256. }
  257. } else if (S_ISDIR(tmp_inode->i_mode)) {
  258. cFYI(1, ("Directory inode"));
  259. tmp_inode->i_op = &cifs_dir_inode_ops;
  260. tmp_inode->i_fop = &cifs_dir_ops;
  261. } else if (S_ISLNK(tmp_inode->i_mode)) {
  262. cFYI(1, ("Symbolic Link inode"));
  263. tmp_inode->i_op = &cifs_symlink_inode_ops;
  264. } else {
  265. cFYI(1, ("Init special inode"));
  266. init_special_inode(tmp_inode, tmp_inode->i_mode,
  267. tmp_inode->i_rdev);
  268. }
  269. }
  270. static void unix_fill_in_inode(struct inode *tmp_inode,
  271. FILE_UNIX_INFO *pfindData, unsigned int *pobject_type, int isNewInode)
  272. {
  273. loff_t local_size;
  274. struct timespec local_mtime;
  275. struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
  276. struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
  277. __u32 type = le32_to_cpu(pfindData->Type);
  278. __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes);
  279. __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
  280. cifsInfo->time = jiffies;
  281. atomic_inc(&cifsInfo->inUse);
  282. /* save mtime and size */
  283. local_mtime = tmp_inode->i_mtime;
  284. local_size = tmp_inode->i_size;
  285. tmp_inode->i_atime =
  286. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
  287. tmp_inode->i_mtime =
  288. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastModificationTime));
  289. tmp_inode->i_ctime =
  290. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastStatusChange));
  291. tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions);
  292. /* since we set the inode type below we need to mask off type
  293. to avoid strange results if bits above were corrupt */
  294. tmp_inode->i_mode &= ~S_IFMT;
  295. if (type == UNIX_FILE) {
  296. *pobject_type = DT_REG;
  297. tmp_inode->i_mode |= S_IFREG;
  298. } else if (type == UNIX_SYMLINK) {
  299. *pobject_type = DT_LNK;
  300. tmp_inode->i_mode |= S_IFLNK;
  301. } else if (type == UNIX_DIR) {
  302. *pobject_type = DT_DIR;
  303. tmp_inode->i_mode |= S_IFDIR;
  304. } else if (type == UNIX_CHARDEV) {
  305. *pobject_type = DT_CHR;
  306. tmp_inode->i_mode |= S_IFCHR;
  307. tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
  308. le64_to_cpu(pfindData->DevMinor) & MINORMASK);
  309. } else if (type == UNIX_BLOCKDEV) {
  310. *pobject_type = DT_BLK;
  311. tmp_inode->i_mode |= S_IFBLK;
  312. tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
  313. le64_to_cpu(pfindData->DevMinor) & MINORMASK);
  314. } else if (type == UNIX_FIFO) {
  315. *pobject_type = DT_FIFO;
  316. tmp_inode->i_mode |= S_IFIFO;
  317. } else if (type == UNIX_SOCKET) {
  318. *pobject_type = DT_SOCK;
  319. tmp_inode->i_mode |= S_IFSOCK;
  320. } else {
  321. /* safest to just call it a file */
  322. *pobject_type = DT_REG;
  323. tmp_inode->i_mode |= S_IFREG;
  324. cFYI(1, ("unknown inode type %d", type));
  325. }
  326. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
  327. tmp_inode->i_uid = cifs_sb->mnt_uid;
  328. else
  329. tmp_inode->i_uid = le64_to_cpu(pfindData->Uid);
  330. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
  331. tmp_inode->i_gid = cifs_sb->mnt_gid;
  332. else
  333. tmp_inode->i_gid = le64_to_cpu(pfindData->Gid);
  334. tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks);
  335. spin_lock(&tmp_inode->i_lock);
  336. if (is_size_safe_to_change(cifsInfo, end_of_file)) {
  337. /* can not safely change the file size here if the
  338. client is writing to it due to potential races */
  339. i_size_write(tmp_inode, end_of_file);
  340. /* 512 bytes (2**9) is the fake blocksize that must be used */
  341. /* for this calculation, not the real blocksize */
  342. tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
  343. }
  344. spin_unlock(&tmp_inode->i_lock);
  345. if (S_ISREG(tmp_inode->i_mode)) {
  346. cFYI(1, ("File inode"));
  347. tmp_inode->i_op = &cifs_file_inode_ops;
  348. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  349. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  350. tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
  351. else
  352. tmp_inode->i_fop = &cifs_file_direct_ops;
  353. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  354. tmp_inode->i_fop = &cifs_file_nobrl_ops;
  355. else
  356. tmp_inode->i_fop = &cifs_file_ops;
  357. if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
  358. (cifs_sb->tcon->ses->server->maxBuf <
  359. PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
  360. tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
  361. else
  362. tmp_inode->i_data.a_ops = &cifs_addr_ops;
  363. if (isNewInode)
  364. return; /* No sense invalidating pages for new inode
  365. since we have not started caching readahead
  366. file data for it yet */
  367. if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
  368. (local_size == tmp_inode->i_size)) {
  369. cFYI(1, ("inode exists but unchanged"));
  370. } else {
  371. /* file may have changed on server */
  372. cFYI(1, ("invalidate inode, readdir detected change"));
  373. invalidate_remote_inode(tmp_inode);
  374. }
  375. } else if (S_ISDIR(tmp_inode->i_mode)) {
  376. cFYI(1, ("Directory inode"));
  377. tmp_inode->i_op = &cifs_dir_inode_ops;
  378. tmp_inode->i_fop = &cifs_dir_ops;
  379. } else if (S_ISLNK(tmp_inode->i_mode)) {
  380. cFYI(1, ("Symbolic Link inode"));
  381. tmp_inode->i_op = &cifs_symlink_inode_ops;
  382. /* tmp_inode->i_fop = *//* do not need to set to anything */
  383. } else {
  384. cFYI(1, ("Special inode"));
  385. init_special_inode(tmp_inode, tmp_inode->i_mode,
  386. tmp_inode->i_rdev);
  387. }
  388. }
  389. static int initiate_cifs_search(const int xid, struct file *file)
  390. {
  391. int rc = 0;
  392. char *full_path;
  393. struct cifsFileInfo *cifsFile;
  394. struct cifs_sb_info *cifs_sb;
  395. struct cifsTconInfo *pTcon;
  396. if (file->private_data == NULL) {
  397. file->private_data =
  398. kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
  399. }
  400. if (file->private_data == NULL)
  401. return -ENOMEM;
  402. cifsFile = file->private_data;
  403. cifsFile->invalidHandle = TRUE;
  404. cifsFile->srch_inf.endOfSearch = FALSE;
  405. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  406. if (cifs_sb == NULL)
  407. return -EINVAL;
  408. pTcon = cifs_sb->tcon;
  409. if (pTcon == NULL)
  410. return -EINVAL;
  411. full_path = build_path_from_dentry(file->f_path.dentry);
  412. if (full_path == NULL)
  413. return -ENOMEM;
  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 int 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 SMB end, 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. }