readdir.c 34 KB

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