readdir.c 34 KB

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