readdir.c 34 KB

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