readdir.c 35 KB

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